嵌套列表到数组
Nested list to array
我正在为这个指示我从嵌套列表创建数组的套路而苦恼。我可以找到有关如何使用 Java 执行此操作的解释,但这对我来说仍然有点混乱。
这就是我到目前为止得到的...
function listToArray(list) {
var listArray = [];
for (var i = 0; i < list.length; i++) {
listArray[i] = list.value(i);
};
return listArray;
};
测试用例...
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
Test.assertSimilar(listToArray(list1), [1, 2, 3]);
Test.assertSimilar(listToArray(list2), ["foo", "bar"]);
感谢您的帮助!
用递归做
function listToArray(list) {
var res = [];
Object.keys(list).forEach(function(k) {
if (typeof list[k] == 'object' && list[k] !== null)
[].push.apply(res, listToArray(list[k]));
else if (list[k] !== null)
res.push(list[k]);
});
return res;
};
var list1 = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: null
}
}
};
var list2 = {
value: "foo",
next: {
value: "bar",
next: null
}
};
console.log(listToArray(list1));
console.log(listToArray(list2));
这只是一个简单的链表指针追逐:
function listToArray(list) {
var listArray = [];
while (list !== null) {
listArray.push(list.value);
list = list.next;
}
return listArray;
};
另一个使用递归的解决方案:
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
function convertToArray(list, result) {
result.push(list.value);
list.next && convertToArray(list.next,result);
return result;
}
console.log(convertToArray(list1,[]));
console.log(convertToArray(list2,[]));
您只需要更深入地挖掘对象,直到到达 null
点。
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
function listToArray(list) {
var curr = list, arr = [];
while (curr != null) {
arr.push(curr.value);
curr = curr.next;
}
return arr;
}
console.log(listToArray(list1));
console.log(listToArray(list2));
我正在为这个指示我从嵌套列表创建数组的套路而苦恼。我可以找到有关如何使用 Java 执行此操作的解释,但这对我来说仍然有点混乱。
这就是我到目前为止得到的...
function listToArray(list) {
var listArray = [];
for (var i = 0; i < list.length; i++) {
listArray[i] = list.value(i);
};
return listArray;
};
测试用例...
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
Test.assertSimilar(listToArray(list1), [1, 2, 3]);
Test.assertSimilar(listToArray(list2), ["foo", "bar"]);
感谢您的帮助!
用递归做
function listToArray(list) {
var res = [];
Object.keys(list).forEach(function(k) {
if (typeof list[k] == 'object' && list[k] !== null)
[].push.apply(res, listToArray(list[k]));
else if (list[k] !== null)
res.push(list[k]);
});
return res;
};
var list1 = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: null
}
}
};
var list2 = {
value: "foo",
next: {
value: "bar",
next: null
}
};
console.log(listToArray(list1));
console.log(listToArray(list2));
这只是一个简单的链表指针追逐:
function listToArray(list) {
var listArray = [];
while (list !== null) {
listArray.push(list.value);
list = list.next;
}
return listArray;
};
另一个使用递归的解决方案:
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
function convertToArray(list, result) {
result.push(list.value);
list.next && convertToArray(list.next,result);
return result;
}
console.log(convertToArray(list1,[]));
console.log(convertToArray(list2,[]));
您只需要更深入地挖掘对象,直到到达 null
点。
var list1 = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
var list2 = {value: "foo", next: {value: "bar", next: null}};
function listToArray(list) {
var curr = list, arr = [];
while (curr != null) {
arr.push(curr.value);
curr = curr.next;
}
return arr;
}
console.log(listToArray(list1));
console.log(listToArray(list2));