-1

This is the scenario I am talking about:

let obj = {
      id: "kjhgfr^&*()(*UY",
      id: "kjhgfr^OIJHB",
      id: "kjhgfr^)(*&^%Y",
      id: "DFGHI(*&YTRDTYHKI*",
      id: ")(IUHGVYUJKO))(*UY",
      id: "VGYUKO(*UYH",
      id: "BHYUIOP)(*&^T%",
      id: "0987654567890",
      id: "5678909876543",
    };

I want to create an array with ids like this.

[
  "5678909876543",
  "0987654567890",
  "VGYUKO(*UYH",
  "kjhgfr^&*()(*UY",
  "VGYUKO(*UYH",
];
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Devesh
  • 929
  • 12
  • 10

2 Answers2

2

Your object is incorrect.

Possible it is the same as: Javascript object literal - possible to add duplicate keys?

As a result, it will be override to get the last value.

let obj ={id:"kjhgfr^&*()(*UY",id:"kjhgfr^OIJHB",id:"kjhgfr^)(*&^%Y",id:"DFGHI(*&YTRDTYHKI*",id:")(IUHGVYUJKO))(*UY",id:"VGYUKO(*UYH",id:"BHYUIOP)(*&^T%",id:"0987654567890",id:"5678909876543",};

console.log(obj);
// { "id": "5678909876543"}
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
-1

You cannot have duplicate keys. Each identical key will overwrite the previously defined value.

You could try this instead (assuming you have control over the input):

let obj = {
      id: ["kjhgfr^&*()(*UY",
      "kjhgfr^OIJHB",
      "kjhgfr^)(*&^%Y",
      "DFGHI(*&YTRDTYHKI*",
      ")(IUHGVYUJKO))(*UY",
      "VGYUKO(*UYH",
      "BHYUIOP)(*&^T%",
      "0987654567890",
      "5678909876543"]
    };

This is similar to the following question: Read and loop through an object with non-unique key value pairs