why map() is mutating original objects in a nested array?

Natali

I'm trying to create a new array of objects (usersData) using map() in order to add a new property assigned with a value from a secondary array (countries). During my tests I've noticed that my original array of objects (users) has been modified and the new property (countryName) has been added to it. The same did not happen when I use map to create a new array of objects for my array countries. Could someone tell me what is causing this and help me to understand how to avoid such a behaviour?

const countries = [
  { id: 3, countryName : "UK" },
  { id: 4, countryName : "Spain" },
  { id: 6, countryName : "Germany"}
];

const users = [
  { id : 1,
    name: "Douglas Camp",
    dateOfBirth: "23-06-1984",
    contactDetails:
      {
        country: 3,
        phone: "7373724997"
      }
  },
  {
    id : 2,
    name: "Martin Stein",
    dateOfBirth: "19-08-1992",
    contactDetails:
      {
        country: 6,
        phone: "3334343434"
      }
  },
];

const usersData = users.map(user=> {
  const newUser = {};
  newUser.name = user.name;
  newUser.contactDetails = user.contactDetails;
  newUser.contactDetails.countryName = "UK";
  return newUser;
});

const countriesData = countries.map(country =>
  {
    const newCountry = {};
    newCountry.name = country.countryName;
    newCountry.continent = "Europe";
    return newCountry;
});
console.log(countries);  
console.log(countriesData); 
console.log(users);
console.log(usersData);

I expect the elements in the array users to keep its original structure but it has now the property contactDetails.countryName set to "UK"

Shidersz

On each iteration of users.map() the user.contactDetails holds a reference to an object that you are storing in the new newUser.contactDetails. So both will refer to the same object on memory. On your particular case, you can solve this spreading the user.contactDetails into a new object (like a way of cloning it). But note this will only work for a 1-level deep object. You should search for deep-cloning if your structure is more complicated.

const countries = [{id:3,countryName:"UK"},{id:4,countryName:"Spain"},{id:6,countryName:"Germany"}];

const users = [{id :1,name:"Douglas Camp",dateOfBirth:"23-06-1984",contactDetails:{country:3,phone:"7373724997"}},{id :2,name:"Martin Stein",dateOfBirth:"19-08-1992",contactDetails:{country:6,phone:"3334343434"}},];

const usersData = users.map(user =>
{
    const newUser = {};
    newUser.name = user.name;
    newUser.contactDetails = {...user.contactDetails};
    newUser.contactDetails.countryName = "UK";
    return newUser;
});

const countriesData = countries.map(country =>
{
    const newCountry = {};
    newCountry.name = country.countryName;
    newCountry.continent = "Europe";
    return newCountry;
});

console.log("countries:", countries);  
console.log("countriesData:",countriesData); 
console.log("users:", users);
console.log("usersData:", usersData);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

algorithm to add up objects in an array without mutating the original array

分類Dev

How to pass an array to a function without mutating the original array?

分類Dev

Search functionality without mutating the Original array when template is binded with Original array?

分類Dev

Search nested array of objects

分類Dev

Flattening deeply nested array of objects

分類Dev

Vue access nested array objects

分類Dev

Destruct nested array of objects javascript

分類Dev

Ruby Map Method edits the original array?

分類Dev

lodash map returning array of objects

分類Dev

Convert array of objects with nested array of objects into array-like object

分類Dev

Regroup array of objects based on nested array

分類Dev

change a related field without mutating the original one

分類Dev

How to make an array of multiple nested objects?

分類Dev

Unable to convert a nested object into an array of objects and viceversa

分類Dev

Filtering deeply nested array of objects in Mongo DB

分類Dev

Angular: Nested ngFor on array of objects with FormArray

分類Dev

how to query nested array of objects by also filtering nested array of nested array in mongo

分類Dev

JAXB - how to map xml array to list of Objects

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Why is a BSON serialized numpy array much bigger than the original?

分類Dev

Why does the original array change when I change the copy?

分類Dev

Using array.map() to find elements from an array of objects

分類Dev

Why does the "window" object have so many nested "window" objects?

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

Finding a path to target object in nested array of objects in Javascript

分類Dev

Mongoose Save Nested Array of Objects-Node.js

分類Dev

Search nested array of objects and return full parents as results in JavaScript

分類Dev

Search nested array of objects and return the full path to the object

分類Dev

using decodable to capture array of nested objects in single line

Related 関連記事

  1. 1

    algorithm to add up objects in an array without mutating the original array

  2. 2

    How to pass an array to a function without mutating the original array?

  3. 3

    Search functionality without mutating the Original array when template is binded with Original array?

  4. 4

    Search nested array of objects

  5. 5

    Flattening deeply nested array of objects

  6. 6

    Vue access nested array objects

  7. 7

    Destruct nested array of objects javascript

  8. 8

    Ruby Map Method edits the original array?

  9. 9

    lodash map returning array of objects

  10. 10

    Convert array of objects with nested array of objects into array-like object

  11. 11

    Regroup array of objects based on nested array

  12. 12

    change a related field without mutating the original one

  13. 13

    How to make an array of multiple nested objects?

  14. 14

    Unable to convert a nested object into an array of objects and viceversa

  15. 15

    Filtering deeply nested array of objects in Mongo DB

  16. 16

    Angular: Nested ngFor on array of objects with FormArray

  17. 17

    how to query nested array of objects by also filtering nested array of nested array in mongo

  18. 18

    JAXB - how to map xml array to list of Objects

  19. 19

    How to get the combination of array values from nested arrays in an array of objects

  20. 20

    Why is a BSON serialized numpy array much bigger than the original?

  21. 21

    Why does the original array change when I change the copy?

  22. 22

    Using array.map() to find elements from an array of objects

  23. 23

    Why does the "window" object have so many nested "window" objects?

  24. 24

    javascript return property value from nested array of objects based on condition

  25. 25

    Finding a path to target object in nested array of objects in Javascript

  26. 26

    Mongoose Save Nested Array of Objects-Node.js

  27. 27

    Search nested array of objects and return full parents as results in JavaScript

  28. 28

    Search nested array of objects and return the full path to the object

  29. 29

    using decodable to capture array of nested objects in single line

ホットタグ

アーカイブ