Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda