特定
sessionStorage.cart = "[
{"id":121,"name":"Pants","number":1,"specification":""},
{"id":121,"name":"Pants","number":2,"specification":""},
{"id":121,"name":"Pants","number":3,"specification":""}
]"
我想编写一个函数,找到id为121,Pants名称,数量为2的对象,以便我可以更新该对象的规范.所以我会传递id,名称,数字和所需的新规范值,并获得以下输出:
sessionStorage.cart = "[
{"id":121,"name":"Pants","number":1,"specification":""},
{"id":121,"name":"Pants","number":2,"specification":"new value"},
{"id":121,"name":"Pants","number":3,"specification":""}
]"
我真的在努力思考这个……欢迎指导!
解决方法:
用这个:
var cart = [
{
"id": 121,
"name": "Pants",
"number": 1,
"specification": ""
},
{
"id": 121,
"name": "Pants",
"number": 2,
"specification": ""
},
{
"id": 121,
"name": "Pants",
"number": 3,
"specification": ""
}
];
cart.forEach(function(entry) {
if (entry.id == 121 && entry.name == 'Pants' && entry.number == 2) {
entry.specification = 'new value';
}
});
console.log(cart);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。