0

The idea is to create a function, that takes an object as parameter and returns each property with its type.

const robot = {
  version: 16,
  name: 'Cleaner 3000',
  coords: [345, 12],
};

robotSchema(robot) // [['version', 'number'], ['name', 'string'], ['coords', 'object']]
adiga
  • 34,372
  • 9
  • 61
  • 83

2 Answers2

1

just surround the items you would like to push with the square brackets []

function robotSchema(robot) {
  let arr = [];
 
  for(let key in robot){
    arr.push([key, typeof robot[key]]); // 
  }

  return arr;
}

const robot = { version: 16, name: 'Cleaner 3000', coords: [345, 12], }; 

console.log( robotSchema(robot) )
adiga
  • 34,372
  • 9
  • 61
  • 83
  • [How do I create a runnable stack snippet?](https://meta.stackoverflow.com/questions/358992) – adiga Jul 26 '20 at 10:02
0

You can use Object.keys to loop over your object

const robot = { version: 16, name: 'Cleaner 3000', coords: [345, 12], }; 

const foo = (arr) => {
  return Object.keys(robot).map(rec => {
    return [rec, typeof robot[rec]]
  })
}

console.log(foo(robot))
AlexAV-dev
  • 1,165
  • 4
  • 14