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.

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.