Modify objects in resutl array when combining two arrays into one

foreignChimichurri

I have two arrays of objects (Array1 and Array2) that I must combine in one array (result).

If an value is present in both arrays, it should be only once in the final array.

If a value is not present in any of the arrays, it needs to be added with the boolean set to true.

Here's the situation:

const Array1 = [
{value: "id1", name: "Element1", boolean: false}, 
{value: "id2", name: "Element2", boolean: false}, 
{value: "id3", name: "Element3", boolean: false}
]

const Array2 = [
{value: "id1", name: "Element1", boolean: false}, 
{value: "id2", name: "Element2", boolean: false}, 
{value: "id4", name: "Element4", boolean: false}
]

Expected output:

const result = [
{value: "id1", name: "Element1", boolean: false}, 
{value: "id2", name: "Element2", boolean: false}, 
{value: "id3", name: "Element3", boolean: true}, 
{value: "id4", name: "Element4", boolean: true}
]
decpk

You can easily achieve the result using Map

const Array1 = [
  { value: "id1", name: "Element1", boolean: false },
  { value: "id2", name: "Element2", boolean: false },
  { value: "id3", name: "Element3", boolean: false },
];

const Array2 = [
  { value: "id1", name: "Element1", boolean: false },
  { value: "id2", name: "Element2", boolean: false },
  { value: "id4", name: "Element4", boolean: false },
];

const map = new Map(Array1.map((o) => [o.value, [o]]));
Array2.forEach((o) =>
  map.has(o.value) ? map.get(o.value).push(o) : map.set(o.value, [o])
);
const result = [...map.values()].map((arr) =>
  arr.length > 1 ? arr[0] : { ...arr[0], boolean: true }
);

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining two numpy arrays

How to convert two arrays of same length into one array of objects in javascript?

How to join two arrays into one array of objects

Combining objects on two arrays based on commonly shared ids

Combine two arrays of objects into one array for use in Google Charts

Combining two arrays into a new string array

Combining different properties of two different arrays of objects in one array of objects

JavaScript - "Combining" two similar arrays of objects

Lodash or best approach to combining two arrays of objects with matching key

Combining two functions for working with an array of objects

Combining two arrays into one in Python, by dynamic allocation

Combining two int's arrays into one array by order not working

Combining two arrays in to one in cake php

Underscore.js: Combining two arrays with objects using a function

Two arrays in one array PHP

If two JavaScript arrays of objects share elements, is all non-shared memory garbage-collected when one array is no longer referenced?

Combining two objects by keys whose values are arrays

Combining two arrays with for loops

Combining Two Arrays To One in NodeJS

Non-conformable arrays while combining two 'ts' objects

react-native combining two arrays into one

Merge two arrays in one, combining the data correspondingly

Combining Array of Objects with another Object of Arrays After Component Mounts in React

A lookup function to match the same IDs in two different arrays of objects and inserting key/value pairs into one of the array of objects

Combining two arrays into an object

How to reduce an array of objects with two properties into two arrays, one for each property?

Merge two arrays into one Array of objects React

How to combine two arrays into one array of objects?

Combining two or more arrays into one array according to numeric keys without duplicates