I have this two arrays :
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', location: 67 }
]
const com = [
{ name: "com1", location: 36 },
{ name: "com2", location: 60 }
]
And i want to test if the locationComment in the array com +1 is equal to the locationMethod in the array data, if yes then i want to have something like this :
const array = [
{type: 'type2', name: "com1"},
{type: 'type3', name: "com2"}
]
Here is my code :
const result = data.map((x)=>{
const arr = [];
com.map((y)=>{
if(x.location == y.location+1){
arr.push({
type: x.type,
name: y.name,
location: x.location
})
}
})
return arr;
});
This is the output i get:
[ [],
[ { type: 'type2', name: 'com1', location: 37 } ],
[ { type: 'type3', name: 'com2', location: 61 } ],
[] ]