Use object array to generate a new array dynamically

前端 未结 3 1992
孤街浪徒
孤街浪徒 2021-01-16 14:45

I have a product form which I would like users to add variants of this product to. My object for the main settings

{
  title: \'Bike\',
  settings: {
    opt         


        
3条回答
  •  无人共我
    2021-01-16 15:36

    Try this:

      generateVariants() {
        const options = [...this._form.settings.options];
        const sizeOptions = options[0].values;
        const colorOptions = options[1].values;
        const regeneratedVariants = [];
        for(let i=0;i< sizeOptions.length;i++) {
          for(let j=0;j< colorOptions.length;j++) {
            regeneratedVariants.push({
              Size: sizeOptions[i],
              Color: colorOptions[i],
              Image: "img.png"
            })
          }
        }
        console.log(regeneratedVariants);
      }
    

    this will produce an array with all the variants possible with sizes and colors.

提交回复
热议问题