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']]
1
  • @adiga, I'm not asking a question, but sharing knowledge that can be helpfull to javscript beginner Commented Jul 26, 2020 at 9:59

2 Answers 2

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) )

Sign up to request clarification or add additional context in comments.

1 Comment

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.