Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fetching nested object value

I have this code where it will log the value into the console

const table = { index: 0, 
data: {items:[ 
{students: {name: Gloria, age: 24, gender: female} , id: 025},
{students: {name: Alex, age: 27, gender: male} , id: 024}], 
total: 2}
} 

const result = table.data.items.map(Object.values);

console.log(result);

and the console will appear as

[[{name: Gloria, age: 24, gender: female} , 025],
[{name: Alex, age: 27, gender: male} , id: 024]]

The only problem is I want only the values to appear such as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[{Gloria, 24, female}, 025],
[{Alex, 27, male}, 024]

can somebody tell me what is wrong in here?

>Solution :

for this below

[[{Gloria, 24, female}, 025],
[{Alex, 27, male}, 024]

this object is not valid since Objects should have key-value pairs,

{Gloria, 24, female}

This below is valid

[[{name: Gloria, age: 24, gender: female} , 025],
[{name: Alex, age: 27, gender: male} , id: 024]]

or you can make it like this :

[[[Gloria, 24, female], 025],
[[Alex, 27, male], 024]

Do lemme know if this works, ill let you know how to do that

EDIT: ANSWER :

const table = { index: 0, 
data: {items:[ 
{students: {name: "Gloria", age: 24, gender: "female"} , id: 25},
{students: {name: "Alex", age: 27, gender: "male"} , id: 24}], 
total: 2}
} 
/* 
[[[Gloria, 24, female], 025],
[[Alex, 27, male], 024] */

const result = table.data.items.map((data) => {
const newArr = [];
 newArr.push(data.id)
 const {age,gender,name} = data.students
 const secondArr = [name,age,gender];
newArr.push(secondArr)
return newArr
});

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading