Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  179] [ 3]  / answers: 1 / hits: 34444  / 5 Years ago, wed, january 23, 2019, 12:00:00

I have json:



{
userList:
[{
name: Bob,
age: 28
},{
name: Tom,
age: 45
},{
name: Alice,
age: 32
}]
}


I want to cut only age and put them to array like : public mainChartData1: Array = [28, 45, 32];



I have started to do that by next code:



  const arr = this.users.map(obj => {

var localObj = [];
localObj[obj] = obj.age;
return localObj;

});


But it doesn't work.


More From » arrays

 Answers
4

You can use a little map function to extract the array age





const inputObject = {
userList:
[{
name: Bob,
age: 28
},{
name: Tom,
age: 45
},{
name: Alice,
age: 32
}]
};

const output = inputObject.userList.map(user => user.age);
console.log(output);




[#52731] Thursday, January 17, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
markusdamienn

Total Points: 167
Total Questions: 119
Total Answers: 93

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;