简体   繁体   中英

Multiplying 2 values in nested array then push to a new array - JavaScript Vue

I have an array and I am trying to multiply two values qty and packageQuantity , creating a new array with the result set to the units key.

The trouble is I have a complex nested array and can't workout how to create a new array!

I have figured out how to do the multiplication, however I can't figure out how to push the values to a new array.

I am using Vue, and want to change the values live if possible.

How can I do the multiplication, creating a new array putting the result in the units key?

To clarify, I want to create a new array putting the new result in whilst adding the old values in too. See below.

Thanks so much in advance!

Here is my current array:

data: () => ({
sites: [

  {
    sku: "10001",
    values: [
      {
        variationName: { name: "Unpackaged", qty: 1 },
        units: '',
        packageQuantity: '2'
      }
    ]
  },
  {
    sku: "10002",
    values: [
      {
        variationName: { name: "2 Pack", qty: 2 },
        units: '',
        packageQuantity: '3'
      },
      {
        variationName: { name: "4 Pack", qty: 4 },
        units: '',
        packageQuantity: '1'
      }
    ]
  }
    ]
})

Here is what I want my output to be:

sites: [

  {
    sku: "10001",
    values: [
      {
        variationName: { name: "Unpackaged", qty: 1 },
        units: '2', // 1 * 2
        packageQuantity: '2'
      }
    ]
  },
  {
    sku: "10002",
    values: [
      {
        variationName: { name: "2 Pack", qty: 2 },
        units: '6', // 2 * 3
        packageQuantity: '3'
      },
      {
        variationName: { name: "4 Pack", qty: 4 },
        units: '4', // 1 * 4
        packageQuantity: '1'
      }
    ]
  }
    ]

Here is my calculation:

 sites.forEach(
  (innerArray) => innerArray.values.forEach(
   (item => console.log(item.variationName.qty * item.units))
  )
 )
           let newData=[]; 
            let sites = [

                {
                    sku: "10001",
                    values: [{
                        variationName: {
                            name: "Unpackaged",
                            qty: 1
                        },
                        units: '',
                        packageQuantity: '2'
                    }]
                },
                {
                    sku: "10002",
                    values: [{
                            variationName: {
                                name: "2 Pack",
                                qty: 2
                            },
                            units: '',
                            packageQuantity: '3'
                        },
                        {
                            variationName: {
                                name: "4 Pack",
                                qty: 4
                            },
                            units: '',
                            packageQuantity: '1'
                        }
                    ]
                }
            ]
            for (let i = 0; i < sites.length; i++) {
                let iData = sites[i].values;
                for (let j = 0; j < iData.length; j++) {
                    iData[j].units = parseInt(iData[j].packageQuantity) * parseInt(iData[j].variationName.qty);
                }

            }
             newData.push(sites);

Output

    newData: [
            {
                sku: "10001",
                values: [{
                    variationName: {
                        name: "Unpackaged",
                        qty: 1
                    },
                    units: 2, 
                    packageQuantity: '2'
                }]
            },
            {
                sku: "10002",
                values: [{
                        variationName: {
                            name: "2 Pack",
                            qty: 2
                        },
                        units: 6,
                        packageQuantity: '3'
                    },
                    {
                        variationName: {
                            name: "4 Pack",
                            qty: 4
                        },
                        units: 4, 
                        packageQuantity: '1'
                    }
                ]
            }
        ]

You can take what you've already done and assign it to item.units . But you have to remember to convert the values to integers before the multiplication. You'll also have to convert the result back to a string if you need it as a string. Here's how you would do it if all values were numbers:

 sites.forEach(
    (innerArray) => innerArray.values.forEach(item => { 
       item.units = item.variationName.qty * item.units; 
    })
 )

It might be worth noting that what you've called innerArray isn't really an array and actually an object so you might want to name it better for clarity's sake.

Here is what you could do:

const datum = {
  sites: [

    {
      sku: "10001",
      values: [
        {
          variationName: { name: "Unpackaged", qty: 1 },
          units: 5,
          packageQuantity: ""
        }
      ]
    },
    {
      sku: "10002",
      values: [
        {
          variationName: { name: "2 Pack", qty: 2 },
          units: 3,
        },
        {
          variationName: { name: "4 Pack", qty: 4 },
          units: 6,
          packageQuantity: ""
        }
      ]
    }
      ]
  };

const qtyCalculated = datum && datum.sites && datum.sites.reduce((acc, site) => {
  if (!acc["sku"]) {
    acc["sku"] = [];
  }
  acc["sku"].push(site.sku);
  if (!acc["qty"]) {
    acc["qty"] = [];
  }
  acc["qty"].push(...site.values && site.values.reduce((valueAcc, valueItem) => {
    valueAcc.push(valueItem.variationName.qty * parseInt(valueItem.units, 10));
    return valueAcc;
  }, []));
  return acc;
}, []);

console.log(qtyCalculated);

You can add the name and have the quantity calculated individually for every value item too. I have declared the units a number in the datum. If you are using a string, use the parseInt as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM