Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using for loop with push() to create arrays within a new array

I’m working on a project where I need to declare customsItem formatted in a particular way.
The format given is:

var customsItem = {
    "description":"T-Shirt",
    "quantity":20,
    "net_weight":"1",
    "mass_unit":"lb",
    "value_amount":"200",
    "value_currency":"USD",
    "origin_country":"US",
};

In my project however, I have multiple descriptions, so I need to make customsItem an array containing both.
I have array itemInCart =

[
  {
    itemDescription: 't-shirt',
    qty: 1,
    pre_orderQty: 1,
    price: 30,
    weight: 8
  },
  {
    itemDescription: 'pants',
    qty: 0,
    pre_orderQty: 1,
    price: 40,
    weight: 5
  }
]

I need to get these items in the correct format and within an array called customsItem. I thought to do this using a for loop with push(). Currently, I’m not getting anything when I try to console.log(customsItem), so I’m not sure if this is the best way to achieve the results that I am trying to get. I would really appreciate any help or advice on how to correctly get the results that I need. Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const customsItem = [];
for (var item of itemInCart) {
    const items = {
    "description":item.itemDescription,
    "quantity":item.qty + item.pre_orderQty,
    "net_weight":item.weight,
    "mass_unit":"oz",
    "value_amount":item.price,
    "value_currency":"USD",
    "origin_country":"US",
    }
    customItem.push(
        items
     )
}

>Solution :

You are not pushing into the correct array:

const customsItem = [];
for (var item of itemInCart) {
    const items = {
    "description":item.itemDescription,
    "quantity":item.qty + item.pre_orderQty,
    "net_weight":item.weight,
    "mass_unit":"oz",
    "value_amount":item.price,
    "value_currency":"USD",
    "origin_country":"US",
    }
    customItem.push(          <---- needs to be customsItem.push
        items
     )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading