7

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

1
  • objArray.push({id: obj[i]["id"]}) maybe? Commented Oct 2, 2018 at 16:23

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

3

Just add an object literal by pushing:

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

not sure why that would be useful though...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.