如何在嵌套数组中移动元素

时间:2018-04-17 14:33:38

标签: javascript arrays

我想在嵌套数组中移动元素。所以,这是我的数据:

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
        }
      ],
    }
  ]

我期望的输出:

[
  {
    "_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",
  }
]

然后,我的解决代码:

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

是否有任何有效的方法来创建输出,可能使用数组映射或其他任何东西?谢谢。

3 个答案:

答案 0 :(得分:3)

您可以使用Array#reduce展开<Key 1> <Value 1> <Key 2> <Value 2> 并使用Object.assign添加transactions

也用过:

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

答案 1 :(得分:2)

您可以!?reduce添加产品名称的交易

map

这是一个片段:

&#13;
&#13;
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
},[]);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

只需使用js方法即可获得所需的输出

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