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

时间:2019-04-02 04:16:05

标签: javascript arrays object ecmascript-6

这是我的数组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
]

请帮助,非常感谢

1 个答案:

答案 0 :(得分: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);

上面的代码将为您提供以下答案

输出

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