问题描述
特定
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":""}
]"
我真的在努力思考这个......欢迎指导!
1楼
iplus26
4
已采纳
2015-08-07 02:52:08
用这个:
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);
2楼
user633183
1
2015-08-07 03:38:56
使用的简单解决方案
sessionStorage.cart.find(e =>
e.id === 121 &&
e.name === 'Pants' &&
e.number === 2
).specification = 'new value';
console.log(JSON.stringify(sessionStorage.cart, null, ' '));
产量
[
{
"id": 121,
"name": "Pants",
"number": 1,
"specification": ""
},
{
"id": 121,
"name": "Pants",
"number": 2,
"specification": "new value"
},
{
"id": 121,
"name": "Pants",
"number": 3,
"specification": ""
}
]
注意:这需要ES6。
3楼
Neil Villareal
0
2015-08-07 02:59:20
您可能想尝试以下代码:
function update(oldValues, newValues, keyToCompare) {
var keyToCompareLen = keyToCompare.length;
var result = [];
oldValues.forEach(function(oldValue){
newValues.forEach(function(newValue){
var cnt = 0;
keyToCompare.forEach(function(key){
if(newValue[key] == oldValue[key]) {
++cnt;
}
});
if(cnt == keyToCompareLen) {
oldValue = $.extend(true, {}, oldValue, newValue);
}
});
result.push(oldValue);
});
return result;
}
var result = update([
{"id":121,"name":"Pants","number":1,"specification":""},
{"id":121,"name":"Pants","number":2,"specification":""},
{"id":121,"name":"Pants","number":3,"specification":""}
], [
{"id":121,"name":"Pants","number":2,"specification":"test"}
], [
"id",
"name",
"number"
]);
console.log(result);
注意:您需要包含jquery库。 你也可以在这里运行代码
4楼
rrowland
0
2015-08-07 03:42:32
迭代数组可能效率低下。 为了提高性能,您可以对所有索引值进行字符串化,并使用它们将对象存储在散列映射中以便快速查找。 就像是:
function cartItem() {
this.id;
this.name;
this.number;
this.specification;
}
CartItem.hash = function(id, name, number) {
return [id, name, number].join('|');
};
cartItem.prototype.toHash = function() {
return CartItem.hash(this.id, this.name, this.number);
};
var cartMap = cart.reduce(function(map, item) {
var key = item.toHash();
map[key] = item;
}, { });
然后你可以(很快)通过哈希查看项目:
cartMap[CartItem.hash(id, name, number)];
5楼
Alex
0
2015-08-07 03:51:20
试试这个。 只需匹配和更新
$(document).ready(function() { var o = []; o.id = 121; o.name = "Pants"; o.number = 2; updateVal(o); }); function updateVal(o) { var cart = [{ "id": 121, "name": "Pants", "number": 1, "specification": "" }, { "id": 121, "name": "Pants", "number": 2, "specification": "" }, { "id": 121, "name": "Pants", "number": 3, "specification": "" }]; $.each(cart, function(a, b) { if (b.id == o.id && b.name == o.name && b.number == o.number) { b.specification = "new value"; } }); alert("value updated. specification:" + cart[1].specification); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>