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

286
Vistas
How to get elements form object?

I'm struggling with my homework. My task is to get a value of an object element, after typing the key of it. The problem is a key, which doesn't exist in the object. In this case it should be 'not found' shown. I don't have any clue how to do this. Idk, if I could explain it right, so I'll show my code and the expected result. I hope you'll check it.

  let object = {
  a: "hund", b: "katze", c: "maus", d: "elefant", e: "schlange", f: "stachelschwein", g: "affe", h: "giraffe"
}
function getObjectElements(keys) {
  let result = [];

  for (let i = 0; i < keys.length; i++) {
      for (const objectKey in object) {
        if (keys[i] === objectKey) {
          result.push(object[objectKey])
      }
    }
  }
  return result;
}

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you are not comfortable with the ternary operator i.e., ? or the nullish coalescing operator i.e., ?? (or you have not learned either yet in your course, and you think using them would be unexpected), a simple else branch statement that appends "not result" to the result in the case when the object does not contain the key, should make the tests pass:

const object = {
    a: "hund", b: "katze", c: "maus", d: "elefant", e: "schlange", f: "stachelschwein", g: "affe", h: "giraffe"
}

function getObjectElements(keys) {
    const result = [];
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        if (object.hasOwnProperty(key)) {
            result.push(object[key]);
        } else {
            result.push("not found");
        }
    }
    return result;
}

console.log(getObjectElements(['a', 'b', 'z', 'c']));
console.log(getObjectElements(['a', 'a_1', 'a_2', 'a_3', 'b', 'c', 'd']));

about 4 years ago · Juan Pablo Isaza Denunciar

0

For each key take the value from the object, and if it doesn't exist (using ?? operator or the in operator with a ternary) use 'not found' instead:

const object = {"a":"hund","b":"katze","c":"maus","d":"elefant","e":"schlange","f":"stachelschwein","g":"affe","h":"giraffe"}

function getObjectElements(keys) {
  const result = [];

  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
  
    result.push(object[key] ?? 'not found'); // or key in object ? object[key] : 'not found'
  }
  return result;
}

const result = getObjectElements(['a', 'b', 'z', 'c']);

console.log(result);

You can also shorten the code by using Array.map():

const object = {"a":"hund","b":"katze","c":"maus","d":"elefant","e":"schlange","f":"stachelschwein","g":"affe","h":"giraffe"}

const getObjectElements = keys => keys.map(key => object[key] ?? 'not found');

const result = getObjectElements(['a', 'b', 'z', 'c']);

console.log(result);

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