遍历对象并创建具有所需值的对象

时间:2020-07-27 05:42:43

标签: javascript jquery

我正在尝试使用循环将某些数据从另一个对象存储到对象中。 我尝试过

jQuery.each(data.forter_request.cartItems, function(index, value) {
          console.log(value);
});

这是我得到的物体

 0 :  {basicItemData: {…}, deliveryDetails: {…}}
basicItemData:
category: "Select Items - 20% Off"
name: "Feel The Noize Mini Skirt"
price:
amountLocalCurrency: "90"
amountUSD: "90"
currency: "USD"
__proto__: Object
productId: "362412"
quantity: 2
type: "TANGIBLE"

我想以以下形式动态存储此数据

line_items: [
     {
       product_name: 'Feel The Noize Mini Skirt',
       product_id: '362412',
       product_price: 90.00,
       product_quantity: 2
     },
     {
       product_name: 'Pillows, Large (Set of 2)',
       product_id: '15',
       product_price: 68.00,
       product_quantity: 1
     },
   ]

console.log(初始对象) 返回

{productId: "362412", name: "Feel The Noize Mini Skirt", category: "Select Items - 20% Off", quantity: 1, price: {…}, …}
category: "Select Items - 20% Off"
name: "Feel The Noize Mini Skirt"
price: {amountUSD: "45", amountLocalCurrency: "45", currency: "USD"}
productId: "362412"
quantity: 1
type: "TANGIBLE"
他们有什么办法做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用Object.keys()遍历对象键

所以你会有类似的东西

var line_items = [];
var fields = ['name','productId','price','quantity']; // Fields you want

jQuery.each(data.forter_request.cartItems, function(index, value) {
      var initial_object = value.basicItemData;
      var new_object = {};
      Object.keys(initial_object).forEach(function(idx,key){
          // Check if the field is among the fields you want to extract
          if(fields.includes(key)){
              new_object[key] = initial_object[key];
          }
      })
      line_items.push(new_object);
});

您将拥有一个列表,其中包含此格式的对象

{
    name:'',
    productId:'',
    price:'',
    quantity:''
}

您可以通过这种方式使用它或更改属性名称。

答案 1 :(得分:1)

您提到的对象没有正确的格式来创建line_items数组。也许您可以尝试一下。我认为第一个对象应该是对象数组或对象对象,以便我们可以遍历它并构造line_items数组。无论如何,我也会为此添加代码

let myObj = {
  0: {
    basicItemData: {…},
    deliveryDetails: {…}
  },
  basicItemData: null,
  category: "Select Items - 20% Off",
  name: "Feel The Noize Mini Skirt",
  price: "10",
  amountLocalCurrency: "90",
  amountUSD: "90",
  currency: "USD",
  __proto__: Object,
  productId: "362412",
  quantity: 2,
  type: "TANGIBLE",
}


let line_items = [];

let tempObj = {
    product_name: myObj['name'],
    product_id: myObj['productId'],
    product_price: myObj['amountLocalCurrency'],
    product_quantity: myObj['quantity']
  },


  line_items.push(tempObj)

如果您有对象数组,可以尝试一下

假设上面的myObj是一个对象数组,每个对象包含相同的键值对,

//Here we have array of objects which we can loop through
let myObj = [{
    0: {
      basicItemData: {…},
      deliveryDetails: {…}
    },
    basicItemData: null,
    category: "Select Items - 20% Off",
    name: "Feel The Noize Mini Skirt",
    price: "10",
    amountLocalCurrency: "90",
    amountUSD: "90",
    currency: "USD",
    __proto__: Object,
    productId: "362412",
    quantity: 2,
    type: "TANGIBLE",
  },
  {
    0: {
      basicItemData: {…},
      deliveryDetails: {…}
    },
    basicItemData: null,
    category: "different Select Items - 20% Off",
    name: "different name",
    price: "100",
    amountLocalCurrency: "906",
    amountUSD: "190",
    currency: "USD",
    __proto__: Object,
    productId: "362415",
    quantity: 2,
    type: "TANGIBLE",
  },
  { ...
  },
  { ...
  },
]

let line_items = [];


for (item of myObj) {

  let tempObj = {
    product_name: item['name'],
    product_id: item['productId'],
    product_price: item['amountLocalCurrency'],
    product_quantity: item['quantity']
  }
  line_items.push(tempObj)
}

如果结构不同,则需要遍历稍有不同的wat,然后查看This链接。