2

i wanted to move element in nested array. so, here this my data:

let products = [
    {     
      "product_name": "A",
      "_id": "5ace995c14a759325776aab1",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 100,
          "price": 2000
        },
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 80,
          "price": 1500
        },
      ]
    },
    {
      "product_name": "B",
      "_id": "5ace995914a759325776aab0",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f9b",
          "qty": 80,
          "price": 1500
        }
      ],
    }
  ]

The output that i expected:

[
  {
    "_id": "5ad3a274ac827c165a510f99",
    "qty": 100,
    "price": 2000,
    "product_name": "A",
  },
  {
    "_id": "5ad3a274ac827c165a510f99",
    "qty": 80,
    "price": 1500,
    "product_name": "A",
  },
  {
    "_id": "5ad3a274ac827c165a510f9b",
    "qty": 80,
    "price": 1500,
    "product_name": "B",
  }
]

then, my solve code:

function move() {
  var result = []
  for (product of products) {
    for (transaction of product.transactions) {
      transaction.product_name = product.product_name
      result.push(transaction)
    }
  }
  return result
}
product = move()

Is there any effective way to create the output, maybe with array map or anything else? Thank you.

Pengguna
  • 4,636
  • 1
  • 27
  • 32

3 Answers3

4

You could flat the transactions with Array#reduce and using Object.assign for adding product_name.

Also used:

var products = [{ product_name: "A", _id: "5ace995c14a759325776aab1", transactions: [{ _id: "5ad3a274ac827c165a510f99", qty: 100, price: 2000 }, { _id: "5ad3a274ac827c165a510f99", qty: 80, price: 1500 }] }, { product_name: "B", _id: "5ace995914a759325776aab0", transactions: [{ _id: "5ad3a274ac827c165a510f9b", qty: 80, price: 1500 }] }],
    result = products.reduce((r, { transactions, product_name }) =>
        r.concat(transactions.map(t => Object.assign({}, t, { product_name }))),
        []
    );
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can reduce and map the transactions to add the product name

let result = products.reduce((c,v)=>{                            //Loop the array using reduce
    let transactions = v.transactions.map(o=>{                   //Loop thru each transactions using map
        return Object.assign(o,{"product_name":v.product_name}); //Clone the transaction and add the property product_name
    });
    return c.concat(transactions);                               //Merge the current array and the transactions
},[]);

Here is a snippet:

//Your array
let products=[{"product_name":"A","_id":"5ace995c14a759325776aab1","transactions":[{"_id":"5ad3a274ac827c165a510f99","qty":100,"price":2000},{"_id":"5ad3a274ac827c165a510f99","qty":80,"price":1500},]},{"product_name":"B","_id":"5ace995914a759325776aab0","transactions":[{"_id":"5ad3a274ac827c165a510f9b","qty":80,"price":1500}],}]

//The short version
let result = products.reduce((c, v) => c.concat(v.transactions.map(o =>Object.assign(o, {"product_name": v.product_name}))), []);

console.log(result);
Eddie
  • 26,593
  • 6
  • 36
  • 58
1

Just using js methods you can have your desired output

const products = [
    {     
      "product_name": "A",
      "_id": "5ace995c14a759325776aab1",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 100,
          "price": 2000
        },
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 80,
          "price": 1500
        },
      ]
    },
    {
      "product_name": "B",
      "_id": "5ace995914a759325776aab0",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f9b",
          "qty": 80,
          "price": 1500
        }
      ],
    }
  ]
  let output =[];
  products.forEach(elm => elm.transactions.forEach(transaction => {
transaction.product_name = elm.product_name;
output.push(transaction)}));
    console.log(output);

  
Melchia
  • 22,578
  • 22
  • 103
  • 117