简体   繁体   English

当我尝试将它转换为数组时,为什么我的对象的值会被删除?

[英]Why is my object's value delete when I try to turn it to array?

The code below is returning an empty array.下面的代码返回一个空数组。 Why?为什么?

var jsonparse = Array.from(JSON.parse(localStorage.getItem("user")))
console.log(jsonparse)
//it's a object which i turn to string by json.stringify
//but then i want to use it in two form one obj sec array i don't know how to turn it correctly a array
//is retuning me an empty array why? 
//expected output:-
//[{key:"value"}]
//real output
//[]

you can see what is in my localstorage by going to the link of an img (i have took an screen shot of my localstorage):-您可以通过转到 img 的链接来查看我的本地存储中的内容(我已经截取了我的本地存储的屏幕截图):-

https://i.stack.imgur.com/OWZ9u.png https://i.stack.imgur.com/OWZ9u.png

first confirm that localStorage.getItem("user") is an object.首先确认localStorage.getItem("user")是 object。 then change your statement syntax as I have written below.然后按照我在下面写的那样更改您的语句语法。

var jsonparse =  Array.from([JSON.parse(JSON.stringify(localStorage.getItem("user")))])

this will make the array you want:这将使您想要的数组:

var array = [];
array.push(JSON.parse(localStorage.getItem("user")))
console.log(array)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的数组变成了 [object object] - Why did my array turn into [object object] 当我尝试访问特定值时,为什么我的JSON数组响应返回未定义 - Why is my JSON array response returns undefined when I try to access specific value 当我尝试从PHP对象/数组返回值时,出现500错误。 但是可以返回所有对象 - I get the 500 error when I try to return a value from PHP object/array. But it's ok return all the object 为什么当我尝试将字符串添加到我的数组位置时不起作用 - Why is not working when i try to add a to a string to my array location 为什么我所有的我 <LI> 当我在输入框中清除/删除要搜索的值时,仍保持选中状态 - Why do my all my <LI>'s remain selected when I clear/delete the value to search for in my input box 当我尝试在组件上使用数组方法时,为什么我的数组变量未在我的组件中定义? - Why is my array variable undefined in my component when I try to use array methods on it? 当我尝试使用 indexOf 和 splice 删除指定数组时,另一个值被删除 - When I try to delete a specified array using indexOf and splice, another value is deleted 当我尝试显示数组中的 Object 时未定义 - Object in array is undefined when I try to display it 当我尝试将新的 object 添加到我的 state 数组中时,它会将所有现有对象更改为新的 object - When I try to add a new object to my state array, it changes all existing objects to the new object 为什么我的 object 在循环时对它的动态值进行排序 - why my object is sorting it's dynamic value when loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM