我有一个数组objArray
。我想创建一个函数,以便检查是否有另一个具有相同name
键的对象。如果存在,则会在qty
键中添加+1。如果该对象不存在,它会将新对象推送到数组。
var objArray = [
{"name":"bike","color":"blue","qty":2},
{"name":"boat","color":"pink", "qty":1},
];
var carObj = {"name":"car","color":"red","qty":1};
var bikeObj = {"name":"bike","color":"blue","qty":1};
function checkAndAdd (obj) {
for (var i = 0; i < objArray.length; i++) {
if (objArray[i].name === obj.name) {
objArray[i].qty++;
break;
}
else {
objArray.push(obj);
}
};
}
checkAndAdd(carObj);
console.log(objArray);
checkAndAdd(bikeObj);
console.log(objArray);
checkAndAdd(carObj);
之后
console.log(objArray);
应该给予
[
{"name":"car","color":"red", "qty":1},
{"name":"bike","color":"blue","qty":2},
{"name":"boat","color":"pink", "qty":1},
]
fter checkAndAdd(bikeObj);
console.log(objArray);
应该给予
[
{"name":"car","color":"red", "qty":1},
{"name":"bike","color":"blue","qty":3},
{"name":"boat","color":"pink", "qty":1},
]
提前致谢!
答案 0 :(得分:3)
如果找到一个增加数量的项目,则需要检查所有对象并退出该功能。
如果没有找到推动物体。
var objArray = [{ name: "bike", color: "blue", qty: 2 }, { name: "boat", color: "pink", qty: 1 }],
carObj = { name: "car", color: "red", qty: 1 },
bikeObj = { name: "bike", color: "blue", qty: 1 };
function checkAndAdd (obj) {
for (var i = 0; i < objArray.length; i++) {
if (objArray[i].name === obj.name) {
objArray[i].qty++;
return; // exit loop and function
}
}
objArray.push(obj);
}
checkAndAdd(carObj);
console.log(objArray);
checkAndAdd(bikeObj);
console.log(objArray);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 1 :(得分:3)
您还可以使用find
搜索对象属性。
在推送对象之前使用Object.assign
将克隆对象,并且在更改qty
时不会更改原始对象(如果添加更多具有相同名称的对象。)
var objArray = [
{"name":"bike","color":"blue","qty":2},
{"name":"boat","color":"pink", "qty":1},
];
var carObj = {"name":"car","color":"red","qty":1};
var bikeObj = {"name":"bike","color":"blue","qty":1};
function checkAndAdd(obj) {
let o = objArray.find(o => o.name === obj.name); //Find if name exist
if (!o) objArray.push(Object.assign({},obj)); //If not exist, push.
else o.qty += obj.qty; //If exist, add the qty
}
checkAndAdd(carObj);
console.log(objArray);
checkAndAdd(bikeObj);
console.log(objArray);
答案 2 :(得分:1)
使用findIndex在objArray中查找对象是否存在。如果存在,则更新对象,否则推送新对象
var objArray = [{
"name": "bike",
"color": "blue",
"qty": 2
},
{
"name": "boat",
"color": "pink",
"qty": 1
},
];
var carObj = {
"name": "car",
"color": "red",
"qty": 1
};
var bikeObj = {
"name": "bike",
"color": "blue",
"qty": 1
};
function checkAndAdd(obj) {
var x = objArray.findIndex(function(item) {
return item.name === obj.name;
});
if (x === -1) {
objArray.push(obj)
} else {
objArray[x].qty = objArray[x].qty + obj.qty
}
}
checkAndAdd(carObj);
checkAndAdd(bikeObj);
console.log(objArray);
答案 3 :(得分:1)
问题是你在循环中else
,而不是在找到后立即返回,只在循环外调用push()
。
更清洁的方法是:
push()
答案 4 :(得分:1)
足够简单:
function checkAndAdd(obj) {
for (var i=0; i < objArray.length; i++) {
if (objArray[i]name === obj.name) {
objArray[i].qty++;
return
}
}
objArray.push(obj)
}
每次对象名称不匹配时,您的功能都在推送。相反,当此函数匹配名称时,它会增加qty
并停止,然后如果循环结束而没有任何匹配,则会推送obj
。