user13120736
user13120736

Reputation:

Loop through object & create object with required values Jquery

I am trying to store some data into object from another object using loop. i tried

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

Here is the object i am getting

 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"

i want to store this data dynamically in below form

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

{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"

s their any way we can do this ?

Upvotes: 2

Views: 89

Answers (2)

castin
castin

Reputation: 313

You can use Object.keys() to loop through the object keys

So you would have something like

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

You'll have a list with objects in this format

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

You can use it this way or change the property names.

Upvotes: 0

Hadi Mir
Hadi Mir

Reputation: 5133

The object you have mentioned does not have a proper format in order to create the line_items array. Maybe you can try this. I think the first object should be either array of objects or object of objects so that we can loop through it and construct the line_items array. Anyway I will include code for that as well

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)

If you have Array of objects you can try this

Suppose the above myObj is an array of objects and each object contains the same key-value pairs,

//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)
}

If the structure is different and you need to loop through slightly different wat then check out This link.

Upvotes: 0

Related Questions