0

I have 2 arrays

const arrayOne = [
 {id: '110'},
 {id: '202'},
 {id: '259'}
];

const arrayTwo = [
 {data: [{value: 'Alpha', id: '001'}]},
 {data: [{value: 'Bravo', id: '202'}]},
 {data: [{value: 'Charlie', id: '110'}]},
 {data: [{value: 'Delta', id: '202'}]}
];

I need to create a new array comparing arrayOne[idx].id with arrayTwo[idx].data[idx2].id

Upon match, I need to create an array pushing value (arrayTwo[idx].data[idx2].value) to the new array against each index in arrayOne.

In this example, I would get newArray = [null, 'Bravo', null, Delta]

What I have tried:

arrayOne.map(item => ({
   ...item,
   result: arrayTwo.filter(itemTwo => item.data.map(x => x.id).includes(itemTwo.id))
}));

and also

const newArr = [];
arrayOne.map((item, idx) => {
   if (arrayTwo.filter(itemTwo => itemTwo.data?.map(x => x.id) === item.id)) {
     newArr.push(arrayTwo.data[idx].value);
   } else newArr.push(null);
 });

3 Answers 3

1

To do this you can map arrayTwo and use .find() to search for the ID in arrayOne. I also mapped arrayTwo to the inner object to make the second map more concise.

const arrayOne = [
 {id: '110'},
 {id: '202'},
 {id: '259'}
];

const arrayTwo = [
 {data: [{value: 'Alpha',id: '001'}]},
 {data: [{value: 'Bravo',id: '202'}]}, 
 {data: [{value: 'Charlie',id: '777'}]},
  {data: [{value: 'Delta',id: '202'}]}
];

const result = arrayTwo
  .map(obj => obj.data[0])
  .map(obj => (arrayOne.find(v => v.id === obj.id) && obj.value) || null)
  
console.log(result)

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

5 Comments

That's great, thank you for your answer. What if arrayTwo have multiple matches with arrayOne and a different value for each object? I have updated the arrayTwo in the question to reflect the same. Appreciate your response!
@Aaron I updated the answer. Because these new requirements are pretty different from the original one, next time, I'd say it'd be better to post a new question, and link it in the comments here for us to take a look at it. That way we can maintain the context of the original answers.
really appreciate it brother! I did create a new question, which is related to this same question but it includes more than 1 object in data array. This stackoverflow.com/questions/73095903/… is really my actual question. Thanks a ton!
@Aaron Np problem! Mark this as the correct answer if it satisfies you for this question. If you can promise that the scope won't change again, I'll check out the new question ;P
Done! The only change apart from an additional object in data in that question is that I no longer need to pass null values in place of the arrayOne index. Thank you sir!
0
  • Use map to iterate over each element of arr1 and return a new array.
  • Reassemble the data attribute array of each element in the arr2 array using map and flat
  • When arr1 traverses, you can get the current element id, use filter to filter the combined data array, and return an element array that matches the current element id.
  • Based on the case where the id is not matched, use the optional chain operator to get the value.
  • When returning
    1. if you want to get the element array of the id and value attributes, use conditional (ternary) operator, when it doesn't match, return the original element, when it matches, use spread syntax, copy the current element attribute, and add the value attribute
    2. if you only want to get an array of matching results, just return the value, remember to use the optional chain operator to convert the unmatched value to null.

const arr1 = [
  { id: '110' },
  { id: '202' },
  { id: '259' }
];

const arr2 = [
  { data: [{ value: 'Alpha', id: '001' }] },
  { data: [{ value: 'Bravo', id: '202' }] }
];

const result1 = arr1.map(o1 => {
  const data = arr2.map(o2 => o2.data).flat();
  const value = data.filter(o2 => o2.id === o1.id)[0]?.value;
  return value ? {...o1, value} : o1;
});

const result2 = arr1.map(o1 => {
  const data = arr2.map(o2 => o2.data).flat();
  const value = data.filter(o2 => o2.id === o1.id)[0]?.value;
  return value ?? null;
});

[result1, result2].forEach(r => console.log(JSON.stringify(r)));

Comments

0

You can try this easy line of code :

const arrayOne = [{  id: '110' }, { id: '202' }, { id: '259' }];

const arrayTwo = [{ data: [{ value: 'Alpha', id: '001' }], }, { data: [{ value: 'Bravo', id: '202' }] }];       

let result = arrayOne.map(el => {
    let found = arrayTwo.find(f => f.data.at(0)?.id == el.id)?.data.at(0)?.value;
    return { id: el.id, value: found ?? null};
});

console.log(result);

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.