delete Removes key from 2 objects

party34

I have 2 objects: one of them contains the player list and the other one contains players in loading screen.

When i remove a player from playersNotReady object, it also removes the player from the player list and i don't want that.

var players = {
  1: "Player1",
  2: "Player2"
};
var playersNotReady = players;
delete playersNotReady[1];
console.log(players); // {2: "Player2"}
console.log(playersNotReady); // {2: "Player2"}

Why is this happening and how can i remove player from playersNotReady object without removing them from the actual player list?

Eddie

You are not creating a new variable. You are just pointing by reference. You can use Object.assign to clone/create new object.

var players = {
  1: "Player1",
  2: "Player2"
};
var playersNotReady = Object.assign({}, players);
delete playersNotReady[1];
console.log(players); 
console.log(playersNotReady); 

Doc: Object.assign


One option also, is to use Destructuring Assignment, Like:

var players = {
  1: "Player1",
  2: "Player2"
};
var {1: p1,...playersNotReady} = players; //This will create a new object and remove the 1
console.log(players);
console.log(playersNotReady);

Doc: Spread

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Retrofit 2 removes characters after hostname from base url

From Dev

Inherit from 2 objects

From Dev

How to delete a key from table using CDbMigration

From Dev

On delete actions when a table has a 2 foreign key from 2 different tables

From Dev

How to delete Child or Parent objects from Relationship?

From Dev

Is it possible to delete an instance of a class that automatically removes it from all lists in which it is an element?

From Dev

Delete multiple objects from a SQL Server database

From Dev

Delete images on foreign key from disk

From Dev

Delete a key and value from an OrderedDict

From Dev

Creating Event Listeners to Delete Objects from localStorage

From Dev

Delete duplicate rows from table with no unique key

From Dev

JavaFX delete objects from AnchorPane

From Dev

Delete all Large Objects from PostgreSQL database

From Dev

How to delete Foreigen key constraint from table in yii2?

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

delete from 2 table in sql that have a foreign key in other tables

From Dev

Inherit from 2 objects

From Dev

how to delete from another 2 table using one key?

From Dev

Why not delete objects from Cloud Code?

From Dev

Delete the foreign key from MySQL

From Dev

Why setting the key constraints on column removes "NOT NULL" from it (MySQL)?

From Dev

Creating Event Listeners to Delete Objects from localStorage

From Dev

DB2: Can not delete rows from empty table after it was referenced in foreign key

From Dev

PHP - Subtract value from 2 objects where key = is the same

From Dev

Removing ggplot2 legend removes whole data from the plot

From Dev

Delete from object by key

From Dev

concat 2 objects with arrays and delete duplicates (js)

From Dev

Merge 2 arrays of objects setting key from one and value from the other

From Dev

Not able to delete shape objects from fabric canvas

Related Related

  1. 1

    Retrofit 2 removes characters after hostname from base url

  2. 2

    Inherit from 2 objects

  3. 3

    How to delete a key from table using CDbMigration

  4. 4

    On delete actions when a table has a 2 foreign key from 2 different tables

  5. 5

    How to delete Child or Parent objects from Relationship?

  6. 6

    Is it possible to delete an instance of a class that automatically removes it from all lists in which it is an element?

  7. 7

    Delete multiple objects from a SQL Server database

  8. 8

    Delete images on foreign key from disk

  9. 9

    Delete a key and value from an OrderedDict

  10. 10

    Creating Event Listeners to Delete Objects from localStorage

  11. 11

    Delete duplicate rows from table with no unique key

  12. 12

    JavaFX delete objects from AnchorPane

  13. 13

    Delete all Large Objects from PostgreSQL database

  14. 14

    How to delete Foreigen key constraint from table in yii2?

  15. 15

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  16. 16

    delete from 2 table in sql that have a foreign key in other tables

  17. 17

    Inherit from 2 objects

  18. 18

    how to delete from another 2 table using one key?

  19. 19

    Why not delete objects from Cloud Code?

  20. 20

    Delete the foreign key from MySQL

  21. 21

    Why setting the key constraints on column removes "NOT NULL" from it (MySQL)?

  22. 22

    Creating Event Listeners to Delete Objects from localStorage

  23. 23

    DB2: Can not delete rows from empty table after it was referenced in foreign key

  24. 24

    PHP - Subtract value from 2 objects where key = is the same

  25. 25

    Removing ggplot2 legend removes whole data from the plot

  26. 26

    Delete from object by key

  27. 27

    concat 2 objects with arrays and delete duplicates (js)

  28. 28

    Merge 2 arrays of objects setting key from one and value from the other

  29. 29

    Not able to delete shape objects from fabric canvas

HotTag

Archive