简体   繁体   English

JS拆分数组制作子数组并找到所有数组的联合集

[英]JS split array to make sub-arrays and find a union set of all arrays

I've data in the following format:我有以下格式的数据:

Element 1: "Europe > Germany > Berlin > Main Office" (Object)元素 1:“欧洲 > 德国 > 柏林 > 总部”(对象)

Element 2: "America > United States > Headquarters" (Object)元素 2:“美国 > 美国 > 总部”(对象)

Element 3: "America > United States > NY > Main Office" (Object)元素 3:“America > United States > NY > Main Office”(对象)

Element 4: "America > United States > NY > Sub Office" (Object)元素 4:“美国 > 美国 > 纽约 > 分局”(对象)

Element 5: "United Kingdom > England > London > Main Office" (Object)元素 5:“英国 > 英格兰 > 伦敦 > 总部”(对象)

First I'm trying to split the array based on ">" character, and create a tree of all elements in the following format (vanilla js/es6):首先,我尝试根据“>”字符拆分数组,并以以下格式(vanilla js/es6)创建所有元素的树:

"Europe": {
  "Germany": {
    "Berlin": {
      "Main Office": "Object"
    }
  }
},

My attempt:我的尝试:

Array.prototype.slice.call(elements).forEach((el) => {

    let 
    stringIHave = el.name, // "Europe > Germany > Berlin > Main Office" 
    keywordsArr = stringIHave.split( " > " ),
    arrayTree =  new Array();

    Array.prototype.slice.call(keywordsArr).forEach((subEl) => {
        //arrayTree.push(el.toString()); // Result: {"Europe", "Germany", "Berlin", "Main Office" }  

        // What I'm Seeking:  
        // "Europe": { "Germany": { "Berlin": { "Main Office": "Object" } } },

    });

});

Once we have the data formatted, we need to find the union (or one single array) of all arrays, like below:格式化数据后,我们需要找到所有数组的并集(或单个数组),如下所示:

[
"Europe": {
  "Germany": {
    "Berlin": {
      "Main Office": "Object"
    }
  }
},

"America": {
  "United States": {
    "Headquarters",
    "NY": {
      "Main Office": "Object",
      "Sub Office": "Object"
    }
  }
},

"United Kingdom": {
  "England": {
    "London": {
      "Main Office": "Object"
    }
  }
}
]

I've been trying to using Array.from(new Set(masterArray)) for finding the union of all arrays, but no dice!我一直在尝试使用Array.from(new Set(masterArray))来查找所有数组的并集,但没有骰子!

You can use a recursive function for each string in the array to construct the nested objects.您可以对数组中的每个字符串使用递归函数来构造嵌套对象。

 const data = [ 'Element 1: Europe > Germany > Berlin > Main Office', 'Element 2: America > United States > Headquarters', 'Element 3: America > United States > NY > Main Office', 'Element 4: America > United States > NY > Sub Office', 'Element 5: United Kingdom > England > London > Main Office' ] //remove the string before ':' on all array elements. let data1 = data.map(str => str.slice( str.indexOf(':') +1, -1 )); let arrayTree = data1.map(str => convertToTree(str)) ; console.log(arrayTree); function convertToTree(str) { let places = str.split('>'); if(places.length === 1) { return ({"Main Office": "Object"}); } //the first part of the splited string should be the property and the remaining part will be converted //back to string and sent to the recursive function until the result of the split becomes a single element return ({ [places[0]]:convertToTree(places.slice(1).join('>')) }); }

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

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