【发布时间】:2018-05-09 20:09:08
【问题描述】:
这就是问题所在,这是小提琴:https://jsfiddle.net/6zqco0mj/
const start = [{'a':'b'}, {'b':'c'}, {'c':'d'}, {'d':'e'}]
end =
{a:
{b:
{c:
{
d: {}
}
}
}
}
我有一些代码,但不确定如何深入研究对象
const start = [{'b':'c'}, {'a':'b'}, {'c':'d'}, {'d':'e'}];
const end = {};
function convert(key) {
const obj = getObj(key);
if(obj) {
const temp = {};
temp[obj[key]] = convert(obj[key]);
//findKey(obj[key]);
end[key] = temp;
}
}
function getObj(key) {
const foo = start.find((el, i) => { if(el[key]) { return el[key] } });
return foo;
}
function findKey(k) {
// what goes here?
}
convert('a');
console.log(end);
【问题讨论】:
-
实际需要做什么?或者你想要达到什么目的?
-
数据排序了吗?
-
您想保持原始数组不变吗?我们可以使用和改变原始对象吗?
-
还有
e去哪儿了? -
如果数据也恰好包含
{'e': 'a'}会发生什么?
标签: javascript arrays json data-structures