Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Retrieving values from array throws error javacript

I am having an array with following values:

 [
  {
    'Admin1': {
      id: 'fa1b2731'
     
    },
    'Admin2': {
      id: '7b5ab064'
     
    },
    'Admin3': {
      id: '9f462511'
     
    },
    'Admin4': {
      id: 'aa82421d'
    
    },
    'Admin5': {
      id: '34cb2b'
     
    },
    'Admin6': {
      id: 'ff71ffdd'
    
    },
    'Admin7': {
      id: 'b57ac9e7'    
      
    }
  }
] 

The code i am trying to retrieve each user id from above array is throwing an error->expected undefined not to be undefined Following is the code snippet:

 if (userArray) {    
    for (const user of Object.values(userArray)) {
      const delUserRes = await userApi.deleteUserById({
        token: accessToken,
        organizationId: orgid;,
        userId: user.id
      });

the above method reads the userarray corectly but never assign each id to userId form user.id and throws error

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The array in example is having one item, what i mean to get user.id you should call array[0].['Admin1'].id. In your code you doing it like array.['Admin1'].id, so thats why it can't find user.id.

try something like this

 if (userArray) {    
    for (const user of Object.values(userArray[0])) {
      const delUserRes = await userApi.deleteUserById({
        token: accessToken,
        organizationId: orgid;,
        userId: user.id
      });
about 4 years ago · Juan Pablo Isaza Report

0

Your all the user are in single element of array object at 0 index. try below code

 for (const user of Object.values(userArray[0])) {
console.log(user) 
 }

about 4 years ago · Juan Pablo Isaza Report

0

Basically you are trying to get values from an object inside an array, so the Object.values doesn't make sense in your code. You can simply use userArray[0] in your for loop or map like:

var data = [ { 'Admin1': { id: 'fa1b2731' }, 'Admin2': { id: '7b5ab064' }, 'Admin3': { id: '9f462511' }, 'Admin4': { id: 'aa82421d' }, 'Admin5': { id: '34cb2b' }, 'Admin6': { id: 'ff71ffdd' }, 'Admin7': { id: 'b57ac9e7' } } ] 

Object.values(data[0]).map(user => {   //your logic here } );
about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!