11

I tried this approach:

this.plans = [];
this.plansCopy = [...this.plans];

Seems it does not work cause returns duplictions.

1
  • 2
    What do you mean it returns duplications? Commented Dec 12, 2017 at 16:03

3 Answers 3

28

The spread operator returns the individual items of the array. If these are already objects, then it's returning the references to those objects. It's the [] part that is creating a new array. Thus you have a new array, but it will still contain the same object references, so this.plans[0].oper() will call this.plansCopy[0].oper() at the same time as well.

Instead you need to clone each individual object. There are a lot of different ways to do this (create deep copy of the array or individual objects). If you only need one level of cloning you can do:

this.plansCopy = this.plans.map(obj => ({...obj}));

This will create a new array where each element is a copy of each object.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you exent answer more, because you missed main
What does it mean? map(obj => ({...obj}));? you mean? map(obj => ({ return obj})); ?
@Alice what do you mean I missed main? Can you update your question with an example of duplications that are returned?
@Alice .map(obj => ({...obj})) is the same as .map(obj => { return {...obj}; })
Ou, sorry, I thought differently about ...obj
|
10

This is well answered here. The main problem is that you can have the issue of copying a reference, rather than duplicating it, at deeper levels of the array. If you know you only have simple objects, you can also use

const newArray = Array.from(oldArray); 

which I find clearer than the spread operator.

However, if you don't know how deep the complexity of your array goes (i.e. how deeply nested you might have a non-simple type) the generic answer seems to be

this.backupData = JSON.parse(JSON.stringify(genericItems));

As this will go all the way down the array's objects, and build them back up -- cloning for the general case.

Comments

0

If you want to clone just the array object container just use slice:

const a = [1,2,3,4];
const b = a.slice(0); // another array object containing the same content

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.