2

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 ?

3
  • Can you clearly specify the object which you want to travesre through, the one mentioned is not in object format, what does basicItemData nand DeliveryDetails, do you need traverse them also. Commented Jul 27, 2020 at 5:59
  • Yes i need the data into "line_items" format Commented Jul 27, 2020 at 6:01
  • Updated question. Commented Jul 27, 2020 at 6:02

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thankyou for the answer to the question.
Hello, it gives me output but how can i make sure the "line_items" has the same format that i want ?
you have to modify "tempObj" accordingly
0

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.

10 Comments

Thanks for the answer to the question, let me check if that works
initial_object.keys is not a function
Could you please log initial_object and value too ? tell me whats in it
Sure, let me check that
Hi @castin i have updated question & added log data for (initial_object)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.