如何从存储在localstorage中的对象访问用于打印的对象属性?例如,如果我只想打印水果的名称。
var fruitBasket = [{"fruit":"Apple","color":"Red","weight":"0.2"},
{"fruit":"Orange","color":"Orange","weight":"0.3"}];
var setFruits = localStorage.setItem('fruits', JSON.stringify(fruitBasket));
var getFruits = localStorage.getItem('fruits');
答案 0 :(得分:0)
因为您对JSON进行了字符串化,所以当您想要将其转换为JSON时,请务必将其转换为JSON var getFruits = JSON.parse( localStorage.getItem('fruits') );
现在您只需访问如下对象:getFruits[0].fruit;
答案 1 :(得分:0)
var getFruits = JSON.parse(localStorage.getItem('fruits'))
然后迭代它并访问fruit
密钥
getFruits.forEach(function(e){console.log(e.fruit)})