猿问

如何创建javascript数组并将另一个数组数据保存到在Javascript中创建的数组

创建一个 javascript 数组并保存另一个数据数组以在 Javascript 中创建一个数组。我已经按照以下提到的方式进行了尝试。


代码:


var vvv=this.new_products.length-this.quote.lines.length;

      let mmm={};

      if(vvv > 0){

        for(var i = 0; i <= vvv-1; i++){

          mmm ={...this.new_products[this.quote.lines.length+i]};

        }

      }

      console.log(mmm);

例子:


this.new_products -> 

    0: {…}

    1: {…}

    2: {…}

    3: {…}

this.quote.lines ->

    0: {…}

    1: {…}

输出应该是 ->


mmm->

    0: {…} (this.new_products[2])

    1: {…} (this.new_products[3])

当前输出->


{product_code: "khdbdvdjlhc de", description: "sscs", note: "csccc", pinned: false, third_party: 0, …}

我试过的代码不起作用。请帮我解决这个问题。


烙印99
浏览 155回答 2
2回答

拉丁的传说

我假设你需要遗体:&nbsp;let m = [];&nbsp;for(var i = 0; i <= vvv - 1; i++){&nbsp; &nbsp; m.push({...this.new_products[this.quote.lines.length+i]});&nbsp;}

繁花如伊

...您可以像这样使用展开运算符创建浅表副本:var array = ["thing1", "thing2"];var array2 = [...array];如果您需要进行深拷贝,最简单的方法之一是使用 lodash 的 cloneDeep() 函数,如下所示:import _ from 'lodash';var array = ["thing1", "thing2"];var array2 = _.cloneDeep(array);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答