Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda