node.js - 将嵌套对象链表转换为数组 Node js

标签 node.js javascript-objects

链表嵌套对象

Input Should be like this

var ii = {"val":"1","next":{"val":"2","next":{"val":"3","next":{"val":"4","next":{"val":"5","next":null}}}}}; 

Output should be like [1,2,3,4,5]

最佳答案

Solution

var ii = { "val": "1", "next": { "val": "2", "next": { "val": "3", "next": { "val": "4", "next": { "val": "5", "next": null } } } } }; 
var arr = [ii.val]
while(ii.next !== null){
    ii = ii.next;
    arr.push(ii.val)
}
console.log(arr)

关于node.js - 将嵌套对象链表转换为数组 Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304970/

相关文章:

node.js - 如何为 80 端口上的网站制作 Node.js Multi-Tenancy ?

javascript - 当模式中的验证匹配 false 时,Mongoose 不会抛出错误

javascript - 将值分配给另一个具有动态分配键的对象内的对象

javascript - 这是在 ES6 中克隆对象的好方法吗?

javascript - 如何访问对象内的对象属性?

javascript - 动态 Javascript 树结构

node.js - Google Action Webhook 实现

javascript - 错误: password authentication failed for user "myuser"

javascript - NodeJS 抛出非法指令 : 4 on Mac OS

javascript - 如何获取 javascript 嵌套对象中最深嵌套对象的键(路径)