Categories
JavaScript Answers

How to merge 2 arrays of JavaScript objects?

To merge 2 arrays of JavaScript objects, we use the spread operator.

For instance, we write

const arr1 = [
  { name: "lang", value: "German" },
  { name: "age", value: "18" },
];
const arr2 = [
  { name: "childs", value: "5" },
  { name: "lang", value: "German" },
];
const arr3 = [...arr1, ...arr2];

to use the spread operator to spread the arr1 and arr2 entries into a new array and assign the array to arr3.

Categories
JavaScript Answers

How to use Lodash to find and return an object from a JavaScript array?

To use Lodash to find and return an object from a JavaScript array, we use the find method.

For instance, we write

const song = _.find(songs, { id });

to call find with songs with { id } to find the object in the songs array with id property equal to id.

Categories
JavaScript Answers

How to get the difference between two arrays of objects in JavaScript?

To get the difference between two arrays of objects in JavaScript, we use the filter and some methods.

For instance, we write

const arrayOne = [
  { value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer" },
  { value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
  { value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
  { value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
  { value: "a63a6f77-c637-454e-abf2-dfb9b543af6c", display: "Ryan" },
];

const arrayTwo = [
  { value: "4a55eff3-1e0d-4a81-9105-3ddd7521d642", display: "Jamsheer" },
  { value: "644838b3-604d-4899-8b78-09e4799f586f", display: "Muhammed" },
  { value: "b6ee537a-375c-45bd-b9d4-4dd84a75041d", display: "Ravi" },
  { value: "e97339e1-939d-47ab-974c-1b68c9cfb536", display: "Ajmal" },
];

const results = arrayOne.filter(
  ({ value: id1 }) => !arrayTwo.some(({ value: id2 }) => id2 === id1)
);

console.log(results);

to call arrayOne.filter with a callback that calls arrayTwo.some to cheeck if there’s any object with id2, which is the _value property of the item in arrayTwo equal to id1, which is the _value property of the object being looped through in arrayOne.

Then an array with the items in arrayOne without the _value property equal to any entry in arrayTwo object’s _value property is returned.

Categories
JavaScript Answers

How to add property to an array of objects with JavaScript?

To add property to an array of objects with JavaScript, we use the map method.

For instance, we write

const mapped = results.map((obj) => ({ ...obj, active: "false" }));

to call results.map with a callback that returns an object with the original object obj‘s properties spread into a new object and we add the active property to it.

Then an array is returned with the original objects in results with the active property added.

Categories
JavaScript Answers

How to join or combine two arrays to concatenate into one JavaScript array?

To join or combine two arrays to concatenate into one JavaScript array, we call the concat method.

For instance, we write

const a = ["a", "b", "c"];
const b = ["d", "e", "f"];
const c = a.concat(b);

to call a.concat with b to return a new array with the entries in a followed by the entries in b.