1

create a javascript array and save another array of data to created an array in Javascript. I have tried it in the following mentioned way.

Code:

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);

example:

this.new_products -> 
    0: {…}
    1: {…}
    2: {…}
    3: {…}
this.quote.lines ->
    0: {…}
    1: {…}

The output should be ->

mmm->
    0: {…} (this.new_products[2])
    1: {…} (this.new_products[3])

Current output->

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

My tried code is not working. Please help me to solve this.

6
  • It's not clear what you want. Give an exemple of input and what would be the desired result for that input. Commented Oct 15, 2020 at 13:53
  • console.log(mmm); this is undefined, mmm is out of the block where it was declared Commented Oct 15, 2020 at 13:59
  • @PedroLima I updated the question with what you expected. Commented Oct 15, 2020 at 13:59
  • @lissettdm I updated the question with the expected output. Commented Oct 15, 2020 at 14:00
  • So, mmm should be this.new_products[2] right? Commented Oct 15, 2020 at 14:12

2 Answers 2

2

I assumed that you need the remains items:

 let m = [];
 for(var i = 0; i <= vvv - 1; i++){
    m.push({...this.new_products[this.quote.lines.length+i]});
 }
Sign up to request clarification or add additional context in comments.

5 Comments

I edited my tried code with your answer. But I am getting a new error now. > Error in v-on handler: "TypeError: Assignment to constant variable."
Yes, replace const mmm={}; for let mmm={};
Now it workes. But now there is another problem. The problem is now always saving inside mmm the last one. If there are 2 changes this adds only the last change. not both changes. How Can I solve this?
Can you add an example with the problem you are facing?
@user_v12 I updated my answer, I assumed that m is type of Array
0

You can create a shallow copy by using the ... spread operator like this:

var array = ["thing1", "thing2"];

var array2 = [...array];

If you need to make a deep copy, one of the easiest method is using lodash's cloneDeep() function like this:

import _ from 'lodash';

var array = ["thing1", "thing2"];

var array2 = _.cloneDeep(array);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.