Home > Back-end >  How to filter array of objects and assign it in new variable list in typescript? [duplicate]
How to filter array of objects and assign it in new variable list in typescript? [duplicate]

Time:09-21

Need to retrieve records of male objects from the list.

list=[
{ id: 1, name: "Sam", sex: "M"},
{ id: 2, name: "Jane", sex: "F"},
{ id: 3, name: "Mark", sex: "M"},
{ id: 4, name: "Mary, sex: "F"},
{ id: 5, name: "Olivia", sex: "F"},
{ id: 6, name: "Bob", sex: "M"}
]

var maleList = ??? //male list

CodePudding user response:

var maleList = list.filter( x => x.sex === 'M');

CodePudding user response:

var result = list.filter((x) => return x.sex == "M"); refer to below link for more details https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

  • Related