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

284
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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