最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - how to get specific key value from object in javascript? - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Oct 2, 2018 at 19:07 Roy Scheffers 3,90811 gold badges33 silver badges36 bronze badges asked Oct 2, 2018 at 16:22 Jane Jane 1391 gold badge1 silver badge11 bronze badges 1
  • objArray.push({id: obj[i]["id"]}) maybe? – CollinD Commented Oct 2, 2018 at 16:23
Add a comment  | 

3 Answers 3

Reset to default 9

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);

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);

Just add an object literal by pushing:

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

not sure why that would be useful though...

发布评论

评论列表(0)

  1. 暂无评论