How to complete this task with for of Javascript?

Viewed 21

Iterate over the apartment object using the Object.keys() method and the for...of loop. Write the array of keys of the apartment object's own properties in the keys variable, and add all the values of its properties to the values array.

1 Answers

Do you mean this:

const sampleObject = {
  a: 1,
  b: 2,
  c: [],
  d: {},
  e: undefined,
  f: 'string'
}
const keys = [];
const values = [];
const keyAndValue = [];
for (const [key, value] of Object.entries(sampleObject)) {
  keys.push(key);
  values.push(value);
  keyAndValue.push([key, value]);
}

console.log(keys); // ["a","b","c","d","e","f"]
console.log(values); // [1,2,[],{},null,"string"]
console.log(keyAndValue); // [["a",1],["b",2],["c",[]],["d",{}],["e",null],["f","string"]]

Related