简体   繁体   English

在JavaScript中将对象数组转换为嵌套对象的函数

[英]Function to convert an array of objects to nested object in JavaScript

I'm a novice to JS. 我是JS的新手。 Please, help me write a good function for my web application. 请帮我为我的Web应用程序编写一个好的函数。

I have an array of "sayings" – objects kind of 我有一系列的“说法” –对象种类

 story = [
    {   letters:'B',
        head:'heading',
        text:'text',
        img:'img'
    },
    {   letters:'B|A',
        head:'heading',
        text:'text',
        img:'img'
    },
    {   letters:'B|A|E',
        head:'heading',
        text:'text',
        img:'img'
    },
    { 
        letters:'K|A',
        head:'heading',
        text:'text',
        img:'img'
    },
    { 
        letters:'K',
        head:'heading',
        text:'text',
        img:'img'
    }]

The 'letters' property is an address of the "saying" in a nested tree. “字母”属性是嵌套树中“ saying”的地址。 The address consists of any number of capital letters, no numbers or special symbols are allowed. 地址由任意数量的大写字母组成,不允许使用数字或特殊符号。 The | | is a divider. 是一个分隔线。 I need to write a function to convert this array to a tree kind of: 我需要编写一个函数将此数组转换为树型:

  tree = [
    {   letter:'B', 
        letters:'B',
        head:'heading',
        text:'text',
        img:'img'
        nest: [{   letter:'A',
                   letters:'B|A',
                   head:'heading',
                   text:'text',
                   img:'img'
                   nest:[{   letter:'E', 
                              letters:'B|A|E',
                              head:'heading',
                              text:'text',
                              img:'img'
                          }]
               }]

    },
    {   letter:'K',
        letters:'K',
        head:'heading',
        text:'text',
        img:'img'
        nest:[{ letter:'A',
                letters:'K|A',
                head:'heading',
                text:'text',
                img:'img'
              }]
    }]

I understand that recursive function is needed here, but it's too difficult for me to figure out how the function can effectively convert the array without loosing "sayings" with child addresses coming before their parents. 我知道这里需要递归函数,但是我很难弄清楚该函数如何有效地转换数组,而又不会丢失子句出现在父母之前的“说法”。 The function has to be fast for converting big arrays to trees on the fly in AngularJS web application. 该功能必须快速,以便在AngularJS Web应用程序中快速将大数组转换为树。

Thanks for your help! 谢谢你的帮助!

I hope you don't mind I use an object ( {} ) to represent the tree instead of an Array ( [] ), this makes searching for existing letters easier, and since each series of letters only has 1 root (letter), I think this makes sense. 希望您不要介意使用对象( {} )代替树( [] )来表示树,这使搜索现有字母变得更加容易,并且由于每个字母序列只有一个根(字母),我认为这很有道理。 You do not actually need recursion in this case, since your initial entries are just a flat list which we can iterate once. 在这种情况下,您实际上不需要递归,因为您的初始条目只是一个平面列表,我们可以对其进行一次迭代。 I tested this in Node.js which uses the V8 JavaScript engine. 我在使用V8 JavaScript引擎的Node.js中进行了测试。 Using an object with letter keys does make the 'letter' entry in the object itself kind of superfluous by the way. 顺便说一下,使用带有字母键的对象确实会使对象中的“字母”条目变得多余。

var tree = {};

for(var i = 0; i < story.length; i++) {
    var saying = story[i];
    var letters = saying.letters.split('|');

    var search = tree;
    for(var j = 0; j < letters.length; j++) {
        var letter = letters[j];

        var obj = letter in search ? search[letter] : search[letter] = {};

        // Endpoint, assign letter and values to obj
        if(j == letters.length - 1) { 
            obj.letter = letter;
            for(key in saying) {
                obj[key] = saying[key];
            }
        } else { // Create nested object and update search object
            search = 'nest' in obj ? obj.nest : obj.nest = {};
        }
    }
};

// Output:
// { B: 
//    { letter: 'B',
//      letters: 'B',
//      head: 'heading',
//      text: 'text',
//      img: 'img',
//      nest: 
//       { A: 
//          { letter: 'A',
//            letters: 'B|A',
//            head: 'heading',
//            text: 'text',
//            img: 'img',
//            nest: 
//             { E: 
//                { letter: 'E',
//                  letters: 'B|A|E',
//                  head: 'heading',
//                  text: 'text',
//                  img: 'img' } } } } },
//   K: 
//    { nest: 
//       { A: 
//          { letter: 'A',
//            letters: 'K|A',
//            head: 'heading',
//            text: 'text',
//            img: 'img' } },
//      letter: 'K',
//      letters: 'K',
//      head: 'heading',
//      text: 'text',
//      img: 'img' } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM