Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Select Ids from object [duplicate]

Tags:

I am new to Typescript. I want to select ids from observable

This is my observable

let myObj = [{   "id": 1,   "text": "Mary" }, {   "id": 2,   "text": "Nancy" }, {   "id": 3,   "text": "Paul" }, {   "id": 4,   "text": "Cheryl" }, {   "id": 5,   "text": "Frances" }] 

Expected Result :

let selectedIds = [1,2,3,4,5]; 

Can I do this without creating an array and pushing the ids in a for loop.

like image 489
Sathya V Avatar asked Dec 17 '16 10:12

Sathya V


1 Answers

Use Array#map to map one array to another:

const myObj = [{"id":1,"text":"Mary"},{"id":2,"text":"Nancy"},{"id":3,"text":"Paul"},{"id":4,"text":"Cheryl"},{"id":5,"text":"Frances"}];    const selectedIds = myObj.map(({ id }) => id);    console.log(selectedIds);
like image 173
Ori Drori Avatar answered Sep 29 '22 11:09

Ori Drori