1

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 } ],
  [] ]

2 Answers 2

2

Because you aren't sure whether there will be a match for every element in the com array, you should use reduce:

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 } 
];

const output = com.reduce((a, { name, location }) => {
  const found = data.find(item => item.location === location + 1);
  if (found) {
    a.push({ type: found.type, name });
  }
  return a;
}, []);
console.log(output);

If the locations are unique, you could reduce time complexity to O(N) by transforming data into an object indexed by location first:

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 } 
];

const dataByLoc = data.reduce((a, item) => {
  a[item.location] = item;
  return a;
}, {});

const output = com.reduce((a, { name, location }) => {
  const found = dataByLoc[location + 1];
  if (found) {
    a.push({ type: found.type, name });
  }
  return a;
}, []);
console.log(output);

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

4 Comments

what if i want to search by index? something like data[index].find
data[index] will be a possibly matching object, not an array, so .find doesn't make sense. You could do data[index].location === location + 1, which will evaluate to true if the location matches
Works for me jsfiddle.net/mzfvbxcs though it's a pretty strange thing to be doing
now i get it, it's what i need ... thank you for your help
1

If the one line solution doesn't appeal to you, the following code should work too using a combination of .map() and .find() .

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 } 
]

// Final Array Output with merged properties
let finalArr = []

data.forEach(locationMethodObj => {

// Fetching the type
let finalObj = {
type: locationMethodObj.type
}

// Checking if a location match exists 
const foundObj = com.find(locationCommentObj => (locationCommentObj.location + 1) === 
locationMethodObj.location 
)

// If a match is found, then append name and push
if(foundObj){
finalObj.name = foundObj.name
finalArr.push(finalObj)
}
})

console.log(finalArr)

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.