简体   繁体   English

如何使用ES6在数组内设置对象的值?

[英]How to set an Object's values inside an array with ES6?

I'm looking for the quickest and most resource friendly way of setting an Object's values inside an array. 我正在寻找在数组内设置对象值的最快,最资源友好的方法。 Can use ES6 syntax as well. 也可以使用ES6语法。

So let's say I have this array of objects: 所以说我有这个对象数组:

let group = [
    {
        id: 1,
        name: 'Test 1',
        geo: 'Japan',
        car: 'Toyota'
    },
    {
        id: 2,
        name: 'Test 2',
        geo: 'USA',
        car: 'Tesla'
    },
    {
        id: 3,
        name: 'Test 3',
        geo: 'Germany',
        car: 'Audi'
    }
];

And I want to get one of these objects based on user input. 我想根据用户输入获取这些对象之一。 Let's say we want to get the 3rd item's index by Obejct's ID: 假设我们要通过Obejct的ID获取第三项的索引:

let index = group.findIndex(g => g.id === payload.id);

Once I have the index I want to assign new values to this object like this: 有了索引后,我想为该对象分配新值,如下所示:

group[index].id = payload.id;
group[index].name = payload.name;
group[index].geo = payload.geo;
group[index].car = payload.car;

However it is long, ugly, and imagine if you'd have 50+ values inside this object. 但是,这很长,很丑陋,请想象一下您是否可以在此对象中包含50个以上的值。

Question: Is there any shorter and more efficient way of achieving the same? 问题:是否有任何更短,更有效的方法来实现这一目标? Including ES6-ES7 syntaxes also. 还包括ES6-ES7语法。

You could use Array#find and Object.assign for changing the properties of the found object. 您可以使用Array#findObject.assign更改找到的对象的属性。

 var group = [{ id: 1, name: 'Test 1', geo: 'Japan', car: 'Toyota' }, { id: 2, name: 'Test 2', geo: 'USA', car: 'Tesla' }, { id: 3, name: 'Test 3', geo: 'Germany', car: 'Audi' }], payload = { id: 3, name: 'update', geo: 'Germany Bavaria Ingolstadt', car: 'Audi 1' }, object = group.find(({ id }) => id === payload.id); if (object) { Object.assign(object, payload); } console.log(group); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

el = group.find(g => g.id === payload.id);
if (el) { // add this 'if' if you are not sure if payload's id will be in group
  idx = group.indexOf(el);
  group[idx] = {...el, ...payload};
}

Use indexing. 使用索引。 For one-time search&update the Array.find works well. 对于一次搜索和更新,Array.find效果很好。 But if you need to update more than one entry you have to build an array index. 但是,如果您需要更新多个条目,则必须构建一个数组索引。

 (function(){ let group = [{ id: 1, name: 'Test 1', geo: 'Japan', car: 'Toyota' }, { id: 2, name: 'Test 2', geo: 'USA', car: 'Tesla' }, { id: 3, name: 'Test 3', geo: 'Germany', car: 'Audi' }], payload = { id: 3, name: 'update', geo: 'Germany Bavaria Ingolstadt', car: 'Audi 1' }; let groupIndex = {}; group.forEach(x => groupIndex[x.id]=x); let start = new Date().valueOf(); let id = 1; let updates = 1000000; for(let i=0;i<updates;i++) if (groupIndex[id]) Object.assign(groupIndex[id], payload); console.log(updates+" taken "+(new Date().valueOf()-start)+" ms"); })() 

Above given solution is correct, You can also do same using map in a single line (prefer to use lodash map function). 上面给出的解决方案是正确的,您也可以在一行中使用map来做同样的事情(最好使用lodash map功能)。

let group = [{ id: 1, name: 'Test 1', geo: 'Japan', car: 'Toyota' }, { id: 2, name: 'Test 2', geo: 'USA', car: 'Tesla' }, { id: 3, name: 'Test 3', geo: 'Germany', car: 'Audi' }],
payload = { id: 3, name: 'update', geo: 'Germany Bavaria Ingolstadt', car: 'Audi 1' }; 

group = group.map((item) => item.id === payload.id ? payload : item);

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

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