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

271
Visualizações
find value (and key) of an object in array (JS)

I am writing a function that will iterate through an of objects to find if key:value pair exists and if so, it adds into another array (in case there are multiple matches)

The array contains objects. The function takes as argument what key and value you're looking for and then iterates through array till it finds it. As you can see, key is a variable then and my problem is I am not sure how to access object property when referring to as a variable value.

for (let i = 0; i < arr.length; i++) {
    if (arr[i].[key] == value) {
      result.push(arr[i]);
    }
}

I thought maybe putting it in [key] would help, but I was obviously wrong. I tried googling how to do it, but I guess i can't put it in words right .

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are confusing dot notation with bracket notation.

Remove the .; arr[i].[key] == value1 should be arr[i][key] == value.

const arr = [{
  'a': 'b'
}]

key = 'a'
value = 'b'
const result = []

for (let i = 0; i < arr.length; i++) {
  if (arr[i][key] == value) {
    result.push(arr[i]);
  }
}

console.log(result)

You can simplify the code by using filter:

const arr = [{
  'a': 'b'
}]

key = 'a'
value = 'b'

const result = arr.filter(e => e[key] == value)

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

You are almost there, just get rid of the extra . between [i] and [key]:

for (let i = 0; i < arr.length; i++) {
    if (arr[i][key] == value) {
      result.push(arr[i]);
    }
}

However, there would be a simpler and more straight-forward way, using filter:

const result = arr.filter(obj => obj[key] == value)

Example:

const arr = [
  { a: 1, b: 1 },
  { a: 1, b: 2 },
  { a: 2, b: 1 },
  { a: 2, b: 2 }
]
const key = 'b'
const value = 1

const result = arr.filter(obj => obj[key] == value)

console.log(result)
// Output: [{ a: 1, b: 1 }, { a: 2, b: 1 }]

about 4 years ago · Juan Pablo Isaza Relatório

0

  • use Array.prototype.filter()
  • You should know upfront what exact property name you're willing to reference to:

const arr = [
  {name:"Evan", age:28},
  {name:"John", age:23},
  {name:"Anne", age:28},
];


const searchProp = "age";
const searchValue = 28;

const result = arr.filter((user) => user[searchProp] === searchValue);
console.log(result)

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