I am trying to learn arrays using JavaScript, and I came across a situation where I want to map elements from two different arrays.
First array:
json1=[{
name:"abc",
add:"xyz"
},{
name:"mno",
add :"xxy"
}]
Second array
json2=[{
name:"abc",
add:"xyz",
off:"lop"
},{
name:"rag",
add:"vrt",
off:"wwq"
},{
name:"abc",
add:"xyz",
off:"lop"
},{
name:"wal",
add:"rot",
off:"nim"
},{
name:"abc",
add:"ola",
off:"blo"
}]
Both arrays have common name values ('abc'). I loop and map each value of the json1 name to each json2 name.
To make it faster, I sorted the second array by name:
json2.sort(function(x,y){
return x.name>y.name? 1 : x.name<y.name? -1 :0;
});
I still think this may not be so useful, because if we have more data there may be 100 elements or so in each array, it will take some computation time. Also, to avoid page blocking, I delegated all these tasks to the web worker.
Is there any way to do this faster?