简体   繁体   English

向对象数组添加新属性

[英]Add new property to an array of objects

I have a list of entities where each entity is as follows: 我有一个实体列表,其中每个实体如下:

var entity = {
  position: 'X',
  varA: [{ id: 1, name: 'A1' }, { id: 2, name: 'A2' }],
  varB: [{ id: 1, name: 'B1' }, { id: 2, name: 'B2' }],
  varC: [{ id: 1, name: 'C1' }, { id: 2, name: 'C2' }],
}

For each entity I need to add a property 'names' that is: 对于每个实体,我需要添加一个属性“名称”:

entity.names = [varA[0].name, varB[0].name, varC[0].name];

The problem is that varA, varB or varC can in some cases be undefined or have no values. 问题是在某些情况下varA,varB或varC可能是未定义的或没有值。 So I tried: 所以我尝试了:

vm.entities.map(function(entity) { 

  entity.names = [entity.varA[0], entity.varB[0], entity.varC[0]].filter(e => e).map(({name:n}) => n);

  return entity; 

});

The expected output is: 预期的输出是:

entity.names = [ 'A1', 'B1', 'C1' ]

However, I am unable to minify my javascript due to this code line: 但是,由于以下代码行,我无法缩小我的JavaScript:

entity.names = [entity.varA[0], entity.varB[0], entity.varC[0]].filter(e => e).map(({name:n}) => n);

What am I doing wrong and is there a better way to do this? 我做错了什么,还有更好的方法吗?

You could iterate all keys of the object and take only arrays for collecting names for the new property. 您可以迭代对象的所有键,并且仅采用数组来收集新属性的名称。

For getting only one name out of the array, you could use Array#some and exit if a name of an array is found. 为了仅从阵列中获取一个名称,可以使用Array#some并在找到阵列名称时退出。

 var entity = { position: 'X', varA: [{ id: 1, name: 'A1' }, { id: 2, name: 'A2' }], varB: [{ id: 1, name: 'B1' }, { id: 2, name: 'B2' }], varC: [{ id: 1, name: 'C1' }, { id: 2, name: 'C2' }] }; entity.names = Object.keys(entity).reduce(function (r, k) { if (Array.isArray(entity[k])) { entity[k].some(function (o) { if (o && typeof o === 'object' && o.name) { r.push(o.name); return true; } }); } return r; }, []); console.log(entity); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

相关问题 查找具有相同属性值的对象并将它们添加到新的数组属性中 - Find objects with same property value and add them in new array property 将新属性添加到基于单独数组的数组中的对象 - Add new property to objects in an array based of a seperate array 将简单数组转换为对象数组并添加新属性 - Convert simple array into array of objects and add new property 将属性添加到对象数组 - Add property to an array of objects 将具有默认值的新属性添加到 javascript 对象数组 - Add new property with default value to javascript array of objects 无法向JavaScript数组中的对象添加新属性 - Couldn't add new property to objects in JavaScript array 如何根据 ID 检查对象数组并在 JavaScript 中添加新属性 - How to check array of objects based on ID and add new property in JavaScript 根据现有对象的属性值创建新的对象数组 object 将现有对象添加到新数组 - Create a new array of objects by property value from existing object add existing objects to new array 将属性添加到 JavaScript 中的对象数组 - Add property to an array of objects in JavaScript 如何在不具有指定属性的数组中添加某些对象的新属性? (JS) - How to add new property of some objects in the array that doesn't have the specified property? (JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM