如果特定键不存在,则将对象推送到数组

时间:2018-03-01 05:08:00

标签: javascript arrays

我有一系列产品对象,其中包含多个产品详细信息。

var bags = [
    { product_id: 1 , product_name: "product 1" , quantity: 1 },
    { product_id: 2 , product_name: "product 3" , quantity: 1 },
    { product_id: 3 , product_name: "product 2" , quantity: 1 }
];

我可以像这样推送新数组,

 bags.push({ product_id: 4 , product_name: "product 4" , quantity: 1 });

现在我需要在这个数组中推送一个新对象,如果那个新对象product_id已经存在,那么代替添加新项目,已经存在的产品数量,会增加。

假设我将此对象添加到先前已存在于数组中的bags数组中。

{ product_id: 1 , product_name: "product 1" , quantity: 3 }

现在我需要My bags数组,而不是创建新项目

var bags = [
    { product_id: 1 , product_name: "product 1" , quantity: 4 }, // <- updated
    { product_id: 2 , product_name: "product 3" , quantity: 1 },
    { product_id: 3 , product_name: "product 2" , quantity: 1 }
];

5 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var bags = [
            { product_id: 1 , product_name: "product 1" , quantity: 1 },
            { product_id: 2 , product_name: "product 3" , quantity: 1 },
            { product_id: 3 , product_name: "product 2" , quantity: 1 }
        ];    
var data = { product_id: 1 , product_name: "product 1" , quantity: 3 };

bags.map(obj => {
    if(obj.product_id == data.product_id && data.hasOwnProperty('quantity')) {
       obj.quantity += data.quantity;
       return; // Break the loop
    }
});

console.log(bags);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
var bags = [
    { product_id: 1 , product_name: "product 1" , quantity: 1 },
    { product_id: 2 , product_name: "product 3" , quantity: 1 },
    { product_id: 3 , product_name: "product 2" , quantity: 1 }
];

function addProduct(product){
  var found = false;
  for( index in bags){
    if(bags[index].product_id == product.product_id){
        bags[index].quantity += product.quantity;
        found = true;
        break;
    }
  }
  if(!found){
    bags.push(product);
  }
}

var data = { product_id: 1 , product_name: "product 1" , quantity: 3 };

addProduct(data);
console.log(bags);
data = { product_id: 10 , product_name: "product 1" , quantity: 3 };
addProduct(data);
console.log(bags);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你必须使用数组吗?这是将对象用作地图的完美案例。只需使用product_id作为对象的键。很容易搜索。

var bags = {
    1: { product_id: 1 , product_name: "product 1" , quantity: 1 },
    2: { product_id: 2 , product_name: "product 3" , quantity: 1 },
    3: { product_id: 3 , product_name: "product 2" , quantity: 1 }
}

function addProduct(bags, product) {
    var product_id = product.product_id;
    if (product_id in bags) {
        // it's already a product, update quantity
        bags[product_id].quantity += product.quantity;
    } else {
        // it's not a product, add it.
        bags[product_id] = product
    }
}

addProduct(bags, { product_id: 1 , product_name: "product 1" , quantity: 3 })

console.log(bags[1])
// { product_id: 1, product_name: 'product 1', quantity: 4 }

答案 3 :(得分:0)

你可以在推送之前实现一个新的Array原型(或简单地创建一个新函数)进行比较。据我所知,有Array的新原型函数称为find,它有帮助。

&#13;
&#13;
Array.prototype.updatePush = function(obj) {
    const foundItem = this.find((item) =>item.product_id === obj.product_id);
    if (foundItem) {
        foundItem.quantity += obj.quantity;
    } else { 
        this.push(obj);
    }
}
    
bags.updatePush({ product_id: 1 , product_name: "product 1" , quantity: 3 }); // the product_id 1 object should be updated.
&#13;
&#13;
&#13;

答案 4 :(得分:-2)

您需要以某种方式识别要更新的对象。对于更新第一个对象的情况:

bags[0].quantity = 4;