0

I want to get the array of objects created from two simple arrays:

const array1 = [20, 2, 35, 86]
const array2 = [8, 86, 15, 23, 35, 44]

The expected result:

const result = [
 { id: 20, value: false },
 { id: 2, value: false },
 { id: 35, value: true },
 { id: 86, value: true },
];

The array1 length is the one that matters. So I need to find matched values in both arrays as showed in the expected result.

Thank you very much for your help.

deadcoder0904
9,00118 gold badges93 silver badges215 bronze badges
asked May 4, 2020 at 8:57
2
  • what have you tried so far ? can you show us your current code ? Commented May 4, 2020 at 8:59
  • 1
    Please go read How to Ask. You are supposed to show us what you tried, and explain what the specific problem with it was. This is not a code-writing service. Commented May 4, 2020 at 8:59

7 Answers 7

3

You can combine map with includes:

array1.map(i => ({id: i, value: array2.includes(i)}))
answered May 4, 2020 at 9:00
Sign up to request clarification or add additional context in comments.

1 Comment

Neat solution! I think it's worth mentioning that for large arrays it'd be better to use sets for better performance
1

Should be simple. Loop through the first array using Array.map & return an object.

const array1 = [20, 2, 35, 86]
const array2 = [8, 86, 15, 23, 35, 44]
const result = array1.map(i => ({ id: i, value: array2.includes(i) }))
console.log(result)

answered May 4, 2020 at 9:02

Comments

1

Create a set from the second array:

const a2set = new Set(array2);

then map your first array:

array1.map(v1 => ({id:v1, value: a2set.has(v1)}))
answered May 4, 2020 at 9:04

Comments

0

Start a loop against first array and check if that element exists in second array or not.

If element exists push it to array containing objects with flag true or else as false.

const array1 = [20, 2, 35, 86]
const array2 = [8, 86, 15, 23, 35, 44]
var objArray = []
array1.forEach(function(elem){
 objArray.push({
 id : elem,
 value : array2.indexOf(elem) != -1 ? true : false
 });
});
console.log(objArray);

answered May 4, 2020 at 9:00

Comments

0

You can use array indexOf to find if the item is inside the second array.

const array1 = [20, 2, 35, 86];
const array2 = [8, 86, 15, 23, 35, 44];
let output = [];
array1.forEach((number) => {
 output.push({
 id: number,
 value: array2.indexOf(number) !== -1
 });
});
console.log(output);

answered May 4, 2020 at 9:01

Comments

0

Try a simple for loop:

 
const array1 = [20, 2, 35, 86];
const array2 = [8, 86, 15, 23, 35, 44];
var res = [];
for (var i = 0; i < array1.length; i++) {
 if (array2.includes(array1[i])) {
 res.push({ id: array1[i], value: true });
 } else {
 res.push({ id: array1[i], value: false });
 }
}
console.log(res);

answered May 4, 2020 at 9:03

Comments

0

Try the following. If performance is important, or if the arrays might include a large amount of elements, I'd consider using sets for better lookup performance.

const array1 = [20, 2, 35, 86]
const array2 = [8, 86, 15, 23, 35, 44]
const result = array1.map(element => {
 return {
 id: element,
 value: array2.includes(element)
 };
})
answered May 4, 2020 at 9:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.