Node.js将对象的所有属性转换为string
我怎样才能将所有对象属性转换为string,就像它们将是path?
例如
{a:{s:"asd",g:"asd"}, b:2}
输出:
["as", "ag", "b"]
是否存在一个function可以做这样的事情?
没有一个内置到Node中,但recursion编写并不困难:
function descendants(obj) { return Object.keys(obj).map(function (key) { var value = obj[key]; // So as to not include 'a'; a bit of a hack. // You might need better criteria. if (typeof value === 'object') { return descendants(value).map(function (desc) { return key + '.' + desc; }); } return [key]; }).reduce(function (a, b) { return a.concat(b); }); }