-2

I am working on this code below. I am attempting to retrieve only the values, not the keys. What this is not doing is providing me with the values but the keys. Can you help me to get this to work properly? Unfortunately I cannot use the object.values() function, therefore I am at a loss right now. Sorry This was not stated when I posted the original.

function values(obj) {
    let arr = [];
    for (let value in obj) {
      arr.push(value);
    }
    return arr;
  }
let nicknames = {a:`Sunny`, b:`Weirdo`, c:`Chicken`,d:`Tokyo`}
let nicknameValues = values(nicknames)
console.log(nicknameValues)
Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26

2 Answers2

3

You can try:

arr.push(obj[value]);

Demo:

function values(obj) {
    let arr = [];
    for (let value in obj) {
      arr.push(obj[value]);
    }
    return arr;
  }
let nicknames = {a:`Sunny`, b:`Weirdo`, c:`Chicken`,d:`Tokyo`}
let nicknameValues = values(nicknames)
console.log(nicknameValues);

Or

const result = Object.values({a:`Sunny`, b:`Weirdo`, c:`Chicken`,d:`Tokyo`});
console.log(result);

Demo:

const result = Object.values({a:`Sunny`, b:`Weirdo`, c:`Chicken`,d:`Tokyo`});
console.log(result);
Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
2

You can use Object.values:

const values = Object.values({a:`Sunny`, b:`Weirdo`, c:`Chicken`,d:`Tokyo`});

console.log(values);

Or you can change you function values

arr.push(obj[value]);
Mark Partola
  • 654
  • 3
  • 10