简体   繁体   English

搜索和更新嵌套在Javascript数组中的对象的属性

[英]Search & Update properties of an object nested in a Javascript Array

Say I have an array of nested objects like 说我有一组嵌套对象,例如

let vendors = [
    {
        v_id: 'red',
        count: 2,
    },
    {
        v_id: 'blue',
        count: 3,
    },
    {
        v_id: 'green',
        count: 1,
    },
];

And another object "foo" with many properties, one of which is "v_id". 另一个具有许多属性的对象“ foo”,其中一个是“ v_id”。 Based on the value of foo.v_id, I want to either update the counts in the "vendors" array or add a new object to "vendors". 基于foo.v_id的值,我想更新“ vendors”数组中的计数或将一个新对象添加到“ vendors”中。

If foo.v_id matches one of those in the "vendors" array, the corresponding count increases by 1. 如果foo.v_id与“ vendors”数组中的一个匹配,则相应的计数增加1。

Example: 例:

let foo = {
    user_type: 'Other',
    v_id: 'blue'
};

Then "vendors" would become: 然后,“供应商”将变为:

[
    {
        v_id: 'red',
        count: 2,
    },
    {
        v_id: 'blue',
        count: 4,
    },
    {
        v_id: 'green',
        count: 1,
    },
];

Else, if there is no match of v_id, a new object is added to the "vendors" array with the corresponding v_id & count = 1. 否则,如果v_id不匹配,则将新对象添加到具有相应v_id&count = 1的“供应商”数组中。

Example: 例:

let foo = {
    user_type: 'client',
    v_id: 'yellow',
};

Then "vendors" would become: 然后,“供应商”将变为:

[
    {
        v_id: 'red',
        count: 2,
    },
    {
        v_id: 'blue',
        count: 3,
    },
    {
        v_id: 'green',
        count: 1,
    },
    {
        v_id: 'yellow',
        count: 1,
    },
];

How can I efficiently & elegantly do this in Javascript? 如何在Javascript中高效,优雅地做到这一点? I know I can use .filter() to get the specific object in "vendors" that has to be updated but how would I update the "vendors" array itself? 我知道我可以使用.filter()获取必须更新的“供应商”中的特定对象,但是我将如何更新“供应商”数组本身?

Use Array.find() to search for the object in the array by v_id . 使用Array.find()通过v_id在数组中搜索对象。 If the object is in the array, increment count . 如果对象在数组中,请增加count If not push a new object: 如果不推送新对象:

 const vendors = [{"v_id":"red","count":2},{"v_id":"blue","count":3},{"v_id":"green","count":1}]; const foo1 = { user_type: 'Other', v_id: 'blue' }; const foo2 = { user_type: 'client', v_id: 'yellow' }; const addUpdate = (arr, obj) => { const current = arr.find((o) => o.v_id === obj.v_id); if(current) current.count += 1; else arr.push({ v_id: obj.v_id, count: 1 }); }; addUpdate(vendors, foo1); addUpdate(vendors, foo2); console.log(vendors); 

 function upsert(arr, obj){ const index = arr.findIndex(item => item.v_id === obj.v_id); index === -1 ? arr.push({v_id: obj.v_id, count: 1}) : arr[index].count++ } let vendors = [{v_id: 'red',count: 2}, {v_id: 'blue',count: 3}, {v_id: 'green',count: 1}]; let foo1 = {user_type: 'Other', v_id: 'blue'}; let foo2 = {user_type: 'client', v_id: 'yellow'}; upsert(vendors, foo1); upsert(vendors, foo2); console.log(vendors); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM