Jane
Jane

Reputation: 139

how to get specific key value from object in javascript?

I have an object and I would like to create a new object with a specific key value for example,

I have a code as follows:

var objArray = [{
    "firstname": "bbb",
    "userName": "bbb1",
    "title": "",
    "created_by_user_id": "-1",
    "enabled": "true",
    "lastname": "AC",
    "last_connection": "",
    "password": "",
    "manager_id": "0",
    "id": "14",
    "job_title": "job1",
    "last_update_date": "2018-08-08 13:35:56.996"
  },
  {
    "firstname": "aaa",
    "icon": "icons/default/icon_user.png",
    "creation_date": "2018-08-08 13:35:56.876",
    "userName": "aaa1",
    "title": "",
    "created_by_user_id": "-1",
    "enabled": "true",
    "lastname": "AH",
    "last_connection": "",
    "password": "",
    "manager_id": "0",
    "id": "9",
    "job_title": "job2",
    "last_update_date": "2018-08-08 13:35:56.876"
  }
]
for (var i = 0; i < obj.length; i++) {
  objArray.push(obj[i]["id"]);
}
return objArray

I would like to create a new object like

[{
  "id": "14"
}, {

  "id": "9",
}]

But, my result is

[
  "9",
  "3"
]

I would like to get the result as an object. I appreciate any help.

Thanks in advance

Upvotes: 7

Views: 24732

Answers (3)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Just add an object literal by pushing:

objArray.push({ id: obj[i]["id"] });

not sure why that would be useful though...

Upvotes: 3

user4639281
user4639281

Reputation:

Just map the array to an array of new objects containing just the specified key and value:

objArray.map(({ id }) => ({ id }));

Here's an example:

var objArray = [{
    "firstname": "bbb",
    "userName": "bbb1",
    "title": "",
    "created_by_user_id": "-1",
    "enabled": "true",
    "lastname": "AC",
    "last_connection": "",
    "password": "",
    "manager_id": "0",
    "id": "14",
    "job_title": "job1",
    "last_update_date": "2018-08-08 13:35:56.996"
  },
  {
    "firstname": "aaa",
    "icon": "icons/default/icon_user.png",
    "creation_date": "2018-08-08 13:35:56.876",
    "userName": "aaa1",
    "title": "",
    "created_by_user_id": "-1",
    "enabled": "true",
    "lastname": "AH",
    "last_connection": "",
    "password": "",
    "manager_id": "0",
    "id": "9",
    "job_title": "job2",
    "last_update_date": "2018-08-08 13:35:56.876"
  }
];

var processed = objArray.map(({ id }) => ({ id }));
console.log(processed);

Upvotes: 9

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

Use Array.map

var objArray = [{"firstname":"bbb","userName":"bbb1","title":"","created_by_user_id":"-1","enabled":"true","lastname":"AC","last_connection":"","password":"","manager_id":"0","id":"14","job_title":"job1","last_update_date":"2018-08-08 13:35:56.996"},{"firstname":"aaa","icon":"icons/default/icon_user.png","creation_date":"2018-08-08 13:35:56.876","userName":"aaa1","title":"","created_by_user_id":"-1","enabled":"true","lastname":"AH","last_connection":"","password":"","manager_id":"0","id":"9","job_title":"job2","last_update_date":"2018-08-08 13:35:56.876"}];

let result = objArray.map(o => ({id: o.id}));
console.log(result);

Upvotes: 5

Related Questions