gpt4 book ai didi

javascript - 遍历对象中的键值

转载 作者:行者123 更新时间:2023-11-29 18:20:55 24 4
gpt4 key购买 nike

我从本地存储中获取对象数组:

saved_selection = window.localStorage.getItem('match_selection');

console.log('saved selection obj is ');
console.log(saved_selection);

输出是:

{"236":"Forward_2","243":"Forward_1"} 

现在我想遍历并获取键和值。所以我这样做:

for(var index in saved_selection) {
console.log(index + " -> " + saved_selection[index]);
}

但是我得到:

0 -> { 
1 -> "
2 -> 2
3 -> 3

等等,而不是:

236 -> Forward_2
243 -> Forward_1

如何获得所需的输出?

最佳答案

你想解析出localStorage返回的字符串:

saved_selection = JSON.parse( window.localStorage.getItem('match_selection') );

关于javascript - 遍历对象中的键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com