Is there an efficient way to get an array of objects from another array of objects

Sky Lurk

I am building an app and I have an array of objects. The objects have a key where the value is an array of objects with key value pairs. I would like to convert this array into an array of just objects

My first array looks like this:

const firstArray = [
  {
    amount: 343,
    code: "RCU8YI0NKS",
    items: [
      {
        productOne: "crust",
        quantity: 2.2,
      },
      {
        productTwo: "dust",
        quantity: 34,
      },
    ],
    user_id: "wewewefwOHG22323kj",
  },
  {
    amount: 343,
    code: "RCU8YI0NKS",
    items: [
      {
        productTwo: "dust",
        quantity: 32,
      },
      {
        productThree: "must",
        quantity: 34,
      },
    ],
    user_id: "m2LgLRD9MEVNAX56JTpRYDkOOjd2",
  },
];

I would like it to look like this array:

const secondArray = [
  {
    amount: 343,
    code: "RCU8YI0NKS",
    crust: 2.2,
    dust: 34,
    user_id: "wewewefwOHG22323kj",
  },
  {
    amount: 343,
    code: "RCU8YI0NKS",
    dust: 32,
    must: 34,
    user_id: "m2LgLRD9MEVNAX56JTpRYDkOOjd2",
  },
];

If anyone knows how to do this, please help.

Thanks.

Moritz Ringler

Looks like you just have to turn the items into key value pairs and add them to the object:

const flatten = arr => arr.map(({items, ...obj}) => items.reduce((agg, {quantity, ...rest}) => (agg[Object.values(rest)[0]] = quantity, agg), obj))

const firstArray = [
  {
    amount: 343,
    code: "RCU8YI0NKS",
    items: [
      {productOne: "crust", quantity: 2.2,},
      {productTwo: "dust", quantity: 34,},
    ],
    user_id: "wewewefwOHG22323kj",
  },
  {
    amount: 343,
    code: "RCU8YI0NKS",
    items: [
      {productTwo: "dust", quantity: 32,},
      {productThree: "must", quantity: 34,},
    ],
    user_id: "m2LgLRD9MEVNAX56JTpRYDkOOjd2",
  },
];

console.log(flatten(firstArray))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove array of objects from another array of objects

Add JSON object from array of objects into another complex array of objects using JavaScript in efficient way

Efficient way to merge array of objects with similar data

A more efficient way to rearrange Array of Objects?

Efficient way to swap property values in array of objects

Javascript get array of objects from array of objects in array of objects

Is there a way to merge JSON objects array with another JSON objects array from other some other view/table?

How to get values from one array of objects into another array of objects

Javascript: Get the subsets of objects from an array of objects

Lodash get the removed objects from array of objects

What is the most efficient way of accessing a property from an array of objects in PowerShell?

Array of objects from array containing objects with values contained in another array

Javascript - Efficient way to get distinct values from an array of objects

Update objects array from another array

More efficient way search for multiple objects in array

update dictionary in javascrips from array of objects in efficient way

Restructuring JavaScript array of objects in the most efficient way

Efficient way to get top count of an option in an array of objects

Efficient way to group objects in an Array

Most efficient way to get average by object property (array of Objects)

Efficient way to modify an object from array of objects via index and insert new objects in the same array in mongodb?

Efficient way to get unique objects from array of objects, by object property?

Javascript: Filter array of objects based on array values in an efficient way

How to get values from an array, inside another array, with stdClass Objects?

Most efficient way to remove objects from array based on key value objects in another array

Most efficient way to use an array of objects to query more objects - MongoDb?

reconstruct an array of objects from another object array

How to get value from objects in array of objects?

Efficient way to separate an array of objects based on another array