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

376
Visualizações
How to get an object out of an array without knowing its index, only 'key' name

With 2 arrays (one of numbers and other of objects),
How can I get a specific object's-(size) value (in other function) without knowing its index (and it's value) in the array only its key
with the shortest and fastest way so I can use that value number ?

If the array is empty except for this specific object, the size property value will be - 0;

If my question isn't clear please comment and I'll fix it and make it betters.

let firstArray = [ 1, 2 , 10, 23, {size: 890}];
     
   funcExtract(firstArray);
    
   function funcExtract(arr){
        
    // arr ? How to find the object when knowing only its key
    }

And with array of objects

let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
 
      funcExtract(secondArray); 

      function funcExtract(arr){
        
      }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use the Array#find function:

const test = [1, 2, 3, 4, {key: 5}]
const findOne = test.find(item => item.hasOwnProperty("key"))
console.log(findOne)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's the thing which you can do it:

let key = "size"
let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
let firstArray = [ 1, 2 , 10, 23, {size: 890}];
function funcExtract(arr,key){
   let filter = arr.filter((x)=> typeof x === "object" && x.hasOwnProperty(key))
   console.log(filter[0].size)
}
funcExtract(firstArray,key);
funcExtract(secondArray,key); 

about 4 years ago · Juan Pablo Isaza Relatório

0

let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
secondArray.find(o => o.size) // {size: 700}
a.filter(o => o.size) // good if you need to find more than 1 with same key
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