1

This is my JSON file output:

let employees = [{
 "id":1,
 "name":"Lann",
 "username":"brot",
 "email":"[email protected]",
 "address": {
 "city":"Gweh",
 "zipcode":"92998-3874",
 "geo": {
 "lat":"45",
 "lng":"77"
 }
 }
}]

How I get id, name and email from that like below:

{
 "id":1,
 "name":"Lann",
 "email":"[email protected]"
}
axel
4,1575 gold badges52 silver badges81 bronze badges
asked Oct 10, 2021 at 4:56
1
  • Hi ! Your json input is malformed : missing }, ie }] should be }}] Whith a correct input, you can console.log(employees[0].name); for example. Commented Oct 10, 2021 at 5:17

4 Answers 4

3

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

let employees = [{
 "id": 1,
 "name": "Lann",
 "username": "brot",
 "email": "[email protected]",
 "address": {
 "city": "Gweh",
 "zipcode": "92998-3874",
 "geo": {
 "lat": "45",
 "lng": "77"
 }
 }
}];
const picked = (({ id, name, email }) => ({ id, name, email }))(employees[0]);
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

let employees = [{
 "id": 1,
 "name": "Lann",
 "username": "brot",
 "email": "[email protected]",
 "address": {
 "city": "Gweh",
 "zipcode": "92998-3874",
 "geo": {
 "lat": "45",
 "lng": "77"
 }
 }
}];
let employee = employees.find(o => o.name === 'Lann');
let picked = (({ id, name,email }) => ({ id, name,email }))(employee);
console.log(picked);

answered Oct 10, 2021 at 5:21
Sign up to request clarification or add additional context in comments.

Comments

2

You can archive using map.

let employees = [{
 "id":1,
 "name":"Lann",
 "username":"brot",
 "email":"[email protected]",
 "address":{
 "city":"Gweh",
 "zipcode":"92998-3874",
 "geo":{
 "lat":"45",
 "lng":"77"
 }
 }
 }]
const data = employees.map(o => ({ id: o.id, name: o.name, email:o.email }));
console.log(data[0]);

answered Oct 10, 2021 at 5:03

Comments

2

You can simply do this by using array destructuring.

let employees = [{
 "id":1,
 "name":"Lann",
 "username":"brot",
 "email":"[email protected]",
 "address":{
 "city":"Gweh",
 "zipcode":"92998-3874",
 "geo":{
 "lat":"45",
 "lng":"77"
 }
 }}];
// Destructuring array
const [employee] = employees;

** Now from here employee is an object and you can access its property normally as you do with other objects. For getting id, name, username:**

employee.id;
employee.name;
employee.username;
answered Oct 10, 2021 at 11:33

Comments

1

You cans also loop through your input if it contains multiple items and get an array of shrink items :

let employees = [{
 "id": 1,
 "name": "Lann",
 "username": "brot",
 "email": "[email protected]",
 "address": {
 "city": "Gweh",
 "zipcode": "92998-3874",
 "geo": {
 "lat": "45",
 "lng": "77"
 }
 }
}]
let shrink = [];
for (let employee of employees) {
 shrink.push({
 id: employee.id,
 name: employee.name,
 email: employee.email
 });
}
console.log(shrink);

answered Oct 10, 2021 at 5:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.