其思想是创建一个函数,该函数接受一个对象作为参数,并返回每个属性及其类型。
const robot = {
version: 16,
name: 'Cleaner 3000',
coords: [345, 12],
};
robotSchema(robot) // [['version', 'number'], ['name', 'string'], ['coords', 'object']]发布于 2020-07-26 17:52:48
只需用方括号[]将您想要推送的项目括起来
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) )
发布于 2020-07-26 17:57:54
您可以使用Object.keys循环遍历对象
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))
https://stackoverflow.com/questions/63098580
复制相似问题