简体   繁体   English

向另一个数组的对象数组添加属性

[英]Adding a property to an array of objects from another array

I have an array like this with names and address: 我有一个这样的数组,带有名称和地址:

BTDevices = [
{name:"n1", address:"add1"},
{name:"n2", address:"add2"},
{name:"n3", address:"add3"}]

And another array with alias and address: 另一个具有别名和地址的数组:

EqAlias = [
{btAlias:"a1", address:"add0"},
{btAlias:"a2", address:"add2"},
{btAlias:"a3", address:"add9"}]

I want to add btAlias property to all objects in BTDevices and set the value only if the address are the same, for example in this case I want the following result: 我想将btAlias属性添加到BTDevices中的所有对象,并且仅在地址相同的情况下才设置值,例如,在这种情况下,我想要以下结果:

BTDevices: BTDevices:

name:"n1", address:"add1", btAlias:""
name:"n2", address:"add2", btAlias:"a2"
name:"n3", address:"add3", btAlias:""

My first solution was adding btAlias property using forEach and then using two for loops: 我的第一个解决方案是使用forEach添加btAlias属性,然后使用两个for循环:

// Add Alias
this.BTDevices.forEach(function(obj) { obj.btAlias = "" });

// Set Alias
for (let m = 0; m < this.EqAlias.length; m ++)
{
  for (let n = 0; n < this.BTDevices.length; n++)
  {
    if (this.BTDevices[n].address == this.EqAlias[m].address)
      this.BTDevices[n].btAlias = this.EqAlias[m].btAlias;
  }
}

Is there a better way to do the same? 有没有更好的方法可以做到这一点? I guess using forEach 我想用forEach

You can use map and find 您可以使用map find

Use map to loop the array and create a new array. 使用map循环数组并创建一个新数组。

Use find to check if a string is in an array. 使用find检查字符串是否在数组中。

 let BTDevices = [{name:"n1", address:"add1"},{name:"n2", address:"add2"},{name:"n3", address:"add3"}]; let EqAlias = [{btAlias:"a1", address:"add0"},{btAlias:"a2", address:"add2"},{btAlias:"a3", address:"add9"}]; let result = BTDevices.map( v => { v.btAlias = ( EqAlias.find( e => e.address == v.address ) || { btAlias:"" } ).btAlias; return v; }); console.log( result ); 

Please check doc: .map , .find 请检查doc: .map.find

Using forEach instead of for will just replace the two for loop with forEach . 使用forEach而不是for将只需更换两种用于循环forEach We could argue on which is the best between for and forEach but i don't think there's a good answer. 我们可以就forforEach之间的最佳选择争论不休,但我认为没有一个好的答案。 In modern javascript you can also use the for of loop. 在现代javascript中,您还可以使用for of循环。

Your algorithm is the simpliest and it will work. 您的算法是最简单的,它将起作用。

But, if you want to address some performances issues, you should want to know that your algorithm is also the slowest (O(n²) complexity) 但是,如果您要解决一些性能问题,则应该知道您的算法也是最慢的(O(n²)复杂度)

Another way to do that is to store items of BTDevices in a map to find them faster. 另一种方法是将BTDevices项目存储在地图中以更快地找到它们。 Example: 例:

let map = new Map();
BTDevices.forEach(e => map.set(e.address, e));

EqAlias.forEach(e => {
    let device = map.get(e.address);

    if (device) {
        device.btAlias = e.btAlias;
    }
});

The only advantage of this code is that looking for an item in a Map is faster (between O(1) and O(n), it depends of Map implementation). 该代码的唯一优点是在Map中查找项目的速度更快(在O(1)和O(n)之间,这取决于Map实现)。 But you won't see any differences unless you try to manipulate some very big arrays. 但是除非您尝试操作一些非常大的数组,否则您将看不到任何差异。

You could also do something like this. 您也可以这样做。

 var BTDevices = [{name:"n1", address:"add1"},{name:"n2", address:"add2"},{name:"n3", address:"add3"}]; var EqAlias = [{btAlias:"a1", address:"add0"},{btAlias:"a2", address:"add2"},{btAlias:"a3", address:"add9"}]; var EqAliasAdd = EqAlias.map((e)=>e.address); var BTDevicesAdd = BTDevices.map((e)=>e.address); BTDevicesAdd.map(function(i,k) { BTDevices[k].btAlias = ""; if(EqAliasAdd.indexOf(i) >= 0) BTDevices[k].btAlias = EqAlias[k].btAlias; }); console.log(BTDevices); 

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

相关问题 从另一组对象数组中将属性添加到另一个对象数组 - Adding Property to another Array of objects from another set of Array of Objects Javascript:将属性添加到对象数组 - Javascript: Adding a property to an array of objects 承诺并将数组中的元素添加到另一个数组中的对象中 - Promises and adding elements from an array into objects within another array 将匹配的对象添加到另一个数组时,从数组中删除它们 - Remove Matching Objects from Array While Adding them to Another Array 将数组中的项目添加到另一个数组中的对象中 - Adding items from array into objects within another array "根据属性值 lodash 将对象数组中的属性合并到另一个对象中" - Merge property from an array of objects into another based on property value lodash 将数组属性添加到现有对象数组 - Adding an array property to an existing array of objects 在已从对象数组中过滤的 object 中添加属性 - Adding a property inside an object that has been filtered from an array of objects 从数组中的所有javascript对象添加特定属性的值 - Adding values of a particular property from all javascript objects in an array 以编程方式添加对象时如何从对象数组中键入属性 - How to type a property from an array of objects when adding them programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM