Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!