• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

166
Views
Read data from Json and store in variable javascript

I am getting a response of an api request in JSON format which I store in a variable with json data like this structure

const users=    {
    'ABC': {
        id: '123',
        name:'testname',
    },
    'DEF': {
        id = '222',
        name='testname2'
    }
}

I am trying to read each data under users and store in a different variable. One variable lets say name firstVar which will have all date under 'ABC' and secondVar which will have all data under'DEF' which i can then use for further tests. I am not sure how to achieve it. Any help will be appreciable.

almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to get the ids of each object and store them you can map over the Object.values and return an array of ids.

const users = {
  'ABC': {
      id: '123'
  },
  'DEF': {
      id: '222'
  }
};

const ids = Object.values(users).map(el => {
  return el.id;
});

console.log(ids);

almost 3 years ago · Juan Pablo Isaza Report

0

Not sure if this is what you want, your code is cut off. Array.prototype.find function only works for Array.

const users = {
    'ABC': {
        id: '123'
    },
    'DEF': {
        id: '222'
    }
};

const foundId = Object.keys(users).find(username => users[username].id === '123');

users[foundId] // found user.
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error