如何将数组的属性推送到另一个数组?

问题描述 投票:0回答:1

这是我的数组,dataCom

id: "5c9e3f0aa289e606b46a7fdf"
importPrices: 500
name: "Wings spring"
owner: "5c40442b6e4425163915e5b3"
priceBaseOnBOM: 0
productId: "LXCM"
salePrices: 0

我想将一些属性传递给像这样的新数组(组件)

components.push({
          salePrices: dataCom.salePrices,
          productId:dataCom.productId,
          name: dataCom.name,
          amount: dataCom.amount,
          id: dataCom.id})

但这不对。我想要这样的组件

components
[
    id: "5c9e3f0aa289e606b46a7fdf",
    name: "Wings spring",
    productId: "LXCM"
    salePrices: 0
]

请帮助,非常感谢

javascript arrays object ecmascript-6
1个回答
0
投票

试试这个,

var dataCom = {
  id: "5c9e3f0aa289e606b46a7fdf",
  importPrices: 500,
  name: "Wings spring",
  owner: "5c40442b6e4425163915e5b3",
  priceBaseOnBOM: 0,
  productId: "LXCM",
  salePrices: 0
};
var components = [];
var obj= {
  id: dataCom.id,
  name: dataCom.name,
  productId: dataCom.productId,
  salesPrices: dataCom.salePrices
};
for(var b=0; b<keys(obj).length;b++){
    components[keys(obj)[b]] = obj[keys(obj)[b]]
} 
console.log(components);

上面的代码将给你以下答案

OUTPUT

components
[
  id: "5c9e3f0aa289e606b46a7fdf",
  name: "Wings spring",
  productId: "LXCM"
  salePrices: 0
]
© www.soinside.com 2019 - 2024. All rights reserved.