how to get specific key value from object in javascript?
10,413
Solution 1
Just add an object literal by pushing:
objArray.push({ id: obj[i]["id"] });
not sure why that would be useful though...,
Solution 2
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);
Author by
Jane
Updated on June 18, 2022Comments
-
Jane almost 2 years
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