2

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:

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:

// 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

2
  • What's wrong with your current approach? Commented Feb 22, 2018 at 16:07
  • Yes you could use forEach, but the finality has no differences. Using forEach might just be more readable that's all. Commented Feb 22, 2018 at 16:12

3 Answers 3

2

Using forEach instead of for will just replace the two for loop with forEach. We could argue on which is the best between for and forEach but i don't think there's a good answer. In modern javascript you can also use the for of loop.

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)

Another way to do that is to store items of BTDevices in a map to find them faster. 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). But you won't see any differences unless you try to manipulate some very big arrays.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use map and find

Use map to loop the array and create a new array.

Use find to check if a string is in an array.

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

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.