Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy array of objects and make changes without modifying original array

I have an array of objects. I would like to deep copy the array of objects and make some changes to each object. I want to do this without modifying the original array or original objects that were in that array.

This is the way I have done it. However, being new to JavaScript I want to make sure this is a good approach.

Is there a better way to do this?

const users = 
[
    {
        id       : 1,    
        name     : 'Jack',
        approved : false
    },
    {
        id       : 2,    
        name     : 'Bill',
        approved : true
    },
    {
        id       : 3,    
        name     : 'Rick',
        approved : false
    },
    {
        id       : 4,    
        name     : 'Rick',
        approved : true
    }
];


const users2 = 
    users
        .map(
            (u) => 
            {
                return Object.assign({}, u);
            }
        )    
        .map(
            (u) => 
            {
                u.approved = true;
                return u;
            }
        );    


console.log('New users2 array of objects:')
console.log(users2);

console.log('This was original users array is untouched:')
console.log(users);

Output:

New users2 array of objects:
[ { id: 1, name: 'Jack', approved: true },
  { id: 2, name: 'Bill', approved: true },
  { id: 3, name: 'Rick', approved: true },
  { id: 4, name: 'Rick', approved: true } ]
This was original users array is untouched:
[ { id: 1, name: 'Jack', approved: false },
  { id: 2, name: 'Bill', approved: true },
  { id: 3, name: 'Rick', approved: false },
  { id: 4, name: 'Rick', approved: true } ]
like image 368
Stick-With-SQL Avatar asked Aug 04 '17 17:08

Stick-With-SQL


3 Answers

For a single pass, you could use Object.assign with the changed property as well.

const users = [{ id: 1, name: 'Jack', approved: false }, { id: 2, name: 'Bill', approved: true }, { id: 3, name: 'Rick', approved: false }, { id: 4, name: 'Rick', approved: true }];
const users2 = users.map(u => Object.assign({}, u, { approved: true }));

console.log(users2);
console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0; }

UPDATE with spreading properties.

const users = [{ id: 1, name: 'Jack', approved: false }, { id: 2, name: 'Bill', approved: true }, { id: 3, name: 'Rick', approved: false }, { id: 4, name: 'Rick', approved: true }];
const users2 = users.map(u => ({ ...u, approved: true }));

console.log(users2);
console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 72
Nina Scholz Avatar answered Sep 29 '22 20:09

Nina Scholz


Yes that looks good. You could also perform the modification when you are cloning, in order to avoid mapping over the array twice.

const users2 = users.map((u) => {
    const copiedUser = Object.assign({}, u);
    copiedUser.approved = true;
    return copiedUser;
}); 
like image 34
Wolfie Avatar answered Sep 29 '22 19:09

Wolfie


I prefer JSON.stringify and JSON.parse

var users = [ { id: 1, name: 'Jack', approved: false },
{ id: 2, name: 'Bill', approved: true },
{ id: 3, name: 'Rick', approved: false },
{ id: 4, name: 'Rick', approved: true } ];

// user2 will be copy of array users without reference
var users2 = JSON.parse(JSON.stringify(users));
like image 29
Roman Yakoviv Avatar answered Sep 29 '22 21:09

Roman Yakoviv