[英]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 );
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. 我们可以就
for
和forEach
之间的最佳选择争论不休,但我认为没有一个好的答案。 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.