JS - How to add key:value pairs from objects nested in arrays to other objects nested in another array

Elradsoldier

I know it has been countlessly asked and I assure you that I've read a lot of posts, articles, etc., and watched a lot of videos but nothing seems to click.

so there we go :

Here are 2 arrays with partial information about every person


let arr1 = [{id:00, name:Ben, city:Philadelphia}, {id:01, name:Alice, city:Frankfurt}, {id:02, name:Detlef, city:Vienna}]

let arr2 = [{id:02, age:18}, {id:00, age:39}, {id:01, age:75}]

And there is the desired final result: an array including the name, city, and age of each person

let arr3 = [{name:Ben, city:Philadelphia, age:39}, {name:Alice, city:Frankfurt, age:75 }, {name:Detlef, city:Vienna, age:18}]

What's the situation? Two arrays both containing objects. each nested object has an id. That id is the common key in each array of objects.

What do you want to do? : I want to create a third array including information from both arrays (from arr1: name and city; from arr2:age).

What have you tried so far? : I couldn't manage to achieve anything worth showing. this minimal example is intended to show you a simple example of my current situation which is: I've got an array that is in the LocalStorage on one hand and an API on the other, both contain some info regarding particular objects (let's say, persons). I want to create an array that will contain all the information regarding each person for easier manipulation afterward (DOM generation, etc.).

I've managed to store both arrays in two "local" arrays but the problem is still there: I can't figure out how to make an array where items are getting their key/value from two separate sources.

Thank you for your help!

Mina

You can use reduce method on the arr with array as an inital value, and inside try to find the corrospending item with same id and destruct the object from the id and merge the two object with spread operator.

let arr1 = [{id:00, name:'Ben', city: 'Philadelphia' }, {id:01, name:'Alice', city:'Frankfurt'}, {id:02, name:'Detlef', city:'Vienna'}]

let arr2 = [{id:02, age:18}, {id:00, age:39}, {id:01, age:75}]


const result = arr1.reduce((acc, { id: id1, ...rest1 }) => {
  const { id: id2, ...rest2 } = arr2.find(i => i.id === id1)
  acc.push({ ...rest1, ...rest2 })
  return acc;
}, [])

console.log(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to filter an array with complex nested objects based on another array of key/value pairs?

How to convert an array of nested objects to an object with key, value pairs in JavaScript

Add new key with Value to an array of nested Objects

How to filter array of objects with nested arrays of objects based on other arrays

How to compare value with other objects in nested array?

How to Group Array of Nested Objects into Multiple Arrays by Nested Value

How to filter array of objects with nested arrays based on other arrays

make tree from array of objects, keeping other key value pairs

How to get all values from an array of objects with nested arrays of objects?

How to get the value from a nested array with objects

How to recursively add object key-value pairs to an array of objects with child arrays?

How to flatten nested arrays of objects into an array of arrays?

How to update the value into a nested array of objects if value for specific key is same?

How to get the combination of array values from nested arrays in an array of objects

Filtering array of objects with arrays based on nested value

Sort Array of Objects by Nested Key Value

Filter an array of objects by key and nested value in JavaScript

optimize array building from nested arrays of objects

convert an array and an array of arrays into array of objects with key value pairs

How to update a key/value pair in a nested array of objects in Javascript

Denormalize/flatten list of nested objects into dot separated key value pairs

Summing values in nested objects where key value pairs match

Rename nested key in array of objects JS

How to dynamically add data from an array with objects to a nested array?

Convert nested array of objects to object of key-value pairs using reducer

Looking for a way to compare and count all nested objects key/value pairs in an array

How to destructure all of the objects from the nested arrays?

How can I customize a nested serialized list of django model objects to be key/value pairs instead?

reduce array of objects into nested arrays