-2

I have an array of objects:

const ObjArray= [{
            "id": "90e17e10-8f19-4580-98a8-ad05f4ecd988",
            "name": "john",
            "description": "worker",
            "place": "f.1.1",
            ...
},
{
            "id": "90e17e10-8eqw-4sdagfr4ecd9fsdfs",
            "name": "joe",
            "description": "dev",
            "stepType": "d.2.1",
            ...
}
];

I want to filter the array of objects above to return only specific properties from the objects.
Let's say I want to return only the id and the name of every object in a new array that would look like this:

[{
  "id": "90e17e10-8f19-4580-98a8-ad05f4ecd988",
  "name": "john"},
 {
  "id": "90e17e10-8eqw-4sdagfr4ecd9fsdfs",
  "name": "joe" }

I searched about that but I couldn't find out how to get it the way I want.

Tryliom
  • 895
  • 1
  • 12
  • 37
  • 4
    *"thanks in advance"*: excuse me, but you are supposed to research and try first. http://idownvotedbecau.se/noattempt/ It is not enough to say *"I searched but could not find"*, since StackOverflow has many similar questions and answers for them. – trincot Jul 26 '18 at 10:53
  • 2
    Have you try anything ? – Tryliom Jul 26 '18 at 10:53
  • 1
    Use [Array#map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) – barbsan Jul 26 '18 at 10:54
  • 1
    i tried map(a => a.id) but it apears that it is only works on single property and return all the ids in one object – Ammar Arran Jul 26 '18 at 10:57
  • try map(a=>({id:a.id, name: a.name})) then – guest-418 Jul 26 '18 at 11:02
  • @trincot so you assumed that i came directly here and asked. thanks anyway – Ammar Arran Jul 26 '18 at 11:14
  • @AmmarArran, I don't assume anything. But questions should *demonstrate* what you have tried and where you got stuck. Anyway, the question has two parts: how to create a new array from an existing one, and how to create a new object from an existing one. Both questions have been covered extensively on StackOverflow (for example: https://stackoverflow.com/questions/30664997/object-array-clone-with-subset-of-properties and https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) – trincot Jul 26 '18 at 11:40

1 Answers1

3

IMHO, you are looking for something like this using Array#map:

ObjArray= [{
            "id": "90e17e10-8f19-4580-98a8-ad05f4ecd988",
            "name": "john",
            "description": "worker",
            "place": "f.1.1"  },
{
            "id": "90e17e10-8eqw-4sdagfr4ecd9fsdfs",
            "name": "joe",
            "description": "dev",
            "stepType": "d.2.1",}
];

console.log(ObjArray.map(o => ({'id': o['id'], 'name': o['name']})));

If you prefer object destructuring:

ObjArray= [{
            "id": "90e17e10-8f19-4580-98a8-ad05f4ecd988",
            "name": "john",
            "description": "worker",
            "place": "f.1.1"  },
{
            "id": "90e17e10-8eqw-4sdagfr4ecd9fsdfs",
            "name": "joe",
            "description": "dev",
            "stepType": "d.2.1",}
];

console.log(ObjArray.map(({id, name}) => ({id, name})));
BlackBeard
  • 10,246
  • 7
  • 52
  • 62