Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 递归编辑属性名_Javascript_Recursion - Fatal编程技术网

Javascript 递归编辑属性名

Javascript 递归编辑属性名,javascript,recursion,Javascript,Recursion,我有一个JS数据结构,它来自JSON,如下所示: [ { "eid": "T1", "name": "Topic1", "children": [ { "eId": "T1.1", "name": "subtopic1", "children": [] }, {

我有一个JS数据结构,它来自JSON,如下所示:

[
    {
        "eid": "T1",
        "name": "Topic1",
        "children": [
            {
                "eId": "T1.1",
                "name": "subtopic1",
                "children": []
            },
            {
                "eId": "T1.2",
                "name": "subtopic2"
            }
        ]
    },
    {
        "eId": "T2",
        "name": "Topic1",
        "children": []
    }
]
我需要迭代此过程并构建另一个结构,如下所示:

[
    {
        "id": "T1",
        "text": "Topic1",
        "children": [
            {
                "id": "T1.1",
                "text": "subtopic1",
                "children": []
            },
            {
                "id": "T1.2",
                "text": "subtopic2"
            }
        ]
    },
    {
        "id": "T2",
        "text": "Topic1",
        "children": []
    }
]
我的密码在这里

// topics = the first strucutre

var treeData=[]; 
for(var i=0,len=topics.length;i<len;++i)
{
    var topicElements=topics[i];
    var subNodes=[];
    var nodes={};
    nodes['id']=topicElements.eId;
    nodes['text']=topicElements.name;

    for (var j =0;j<topicElements.children.length;++j)
        {
            nodesChildren = topicElements.children;
            position = subNodes.length;
            subNodes[position] = new Object();
            subNodes[position]['id']=nodesChildren[j].eId;
            subNodes[position]['text']=nodesChildren[j].name;
        }
    nodes['children']=subNodes;
    treeData.push(nodes);
}
//主题=第一个结构
var treeData=[];

对于(var i=0,len=topics.length;i类似的内容,可能:

function redefineData(data) {
    var outData = [];
    for (var i = 0; i < data.length; i++) {
        var obj = { id: data[i].eid, text: data[i].name };
        if (data[i].children && data[i].children.length) {
            obj.children = redefineData(data[i].children);
        }
        outData.push(obj);
    }
    return outData;
}

var treeData = redefineData(topics);
函数重定义数据(数据){
var-outData=[];
对于(变量i=0;i
以下是ES6中的一个通用版本,我认为它更简单一些:

const rename key=(旧名,新名)=>(xs)=>
map(({[oldName]:old,children,…rest})=>({
[新名称]:旧,
休息
…(儿童?{儿童:更名基(旧名,新名)(儿童)}:{})
}))
const fixId=renameKey('eId','id')
const data=[{eId:“T1”,name:“Topic1”,children:[{eId:“T1.1”,name:“subtopic1”,children:[]},{eId:“T1.2”,name:“subtopic2”}},{eId:“T2”,name:“Topic1”,children:[]}]
const treeData=fixId(数据)
console.log(treeData)