This is my JSON file output:
JavaScript
x
15
15
1
let employees = [{
2
"id":1,
3
"name":"Lann",
4
"username":"brot",
5
"email":"b@sd.com",
6
"address": {
7
"city":"Gweh",
8
"zipcode":"92998-3874",
9
"geo": {
10
"lat":"45",
11
"lng":"77"
12
}
13
}
14
}]
15
How I get id, name and email from that like below:
JavaScript
1
6
1
{
2
"id":1,
3
"name":"Lann",
4
"email":"b@sd.com"
5
}
6
Advertisement
Answer
If your array has only one element you can just access the info, no need to build another array like this: employees[0].id
, employees[0].name
, employees[0].email
or you can just extract an object using Object Destructuring
JavaScript
1
16
16
1
let employees = [{
2
"id": 1,
3
"name": "Lann",
4
"username": "brot",
5
"email": "b@sd.com",
6
"address": {
7
"city": "Gweh",
8
"zipcode": "92998-3874",
9
"geo": {
10
"lat": "45",
11
"lng": "77"
12
}
13
}
14
}];
15
const picked = (({ id, name, email }) => ({ id, name, email }))(employees[0]);
16
console.log(picked);
but if your array has more employees, i think what you need to do is search by id or name and get back just an object with minimal info, you can do that like this
JavaScript
1
17
17
1
let employees = [{
2
"id": 1,
3
"name": "Lann",
4
"username": "brot",
5
"email": "b@sd.com",
6
"address": {
7
"city": "Gweh",
8
"zipcode": "92998-3874",
9
"geo": {
10
"lat": "45",
11
"lng": "77"
12
}
13
}
14
}];
15
let employee = employees.find(o => o.name === 'Lann');
16
let picked = (({ id, name,email }) => ({ id, name,email }))(employee);
17
console.log(picked);