Home > Net >  How to get elements form object?
How to get elements form object?

Time:12-06

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

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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']));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related