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

237
Visualizações
how to get the value by the help of includes/index of

how to get the value in any way if key does match with "Item"

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        console.log(data[i].includes("Item"));
        //Expecting phone and case
    }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

for-in allows you to loop through the keys in an object. Not to be confused with for-of, which loop through elements in an array.

const data = [{
          "Item-55566": "phone",        
      },
      {
          "Items-44555": "Case",       
      }
 ];
 
for(let datum of data)
{
  for(let key in datum)
  {
    if(key.includes("Item"))
    {
      console.log(datum[key]);
    }
  }
}

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to keep your loop (good that it's only one loop compared to other answers) you can do it with Object.keys and values:

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        if(Object.keys(data[i])[0].includes('Item')){
            console.log(Object.values(data[i])[0]);
        }
    }

about 4 years ago · Juan Pablo Isaza Relatório

0

In the simple way just change data[i].includes("Item") to data[i].keys().includes("Item").

BUT! Could we have some alternative data set here? For example:

const data = [{
        "Item-55566": "phone",
        "SomeKey: "Some Value",
        123123: "Numeric key with value"
    },
    {
        "Items-44555": "Case",
        "Another-key": "Another value"
    }
];

In this case you need to put some changes in your code to find correct keys & values:

for(let i = 0; i<data.length; i++) {
    data[i].keys().forEach(v=>{ 
      String(v).includes("Item") && console.log("Got index: ${i}, key: ${v}, value: ${data[i][v]}") 
    })

}
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