gpt4 book ai didi

javascript 复杂递归

转载 作者:行者123 更新时间:2023-12-02 18:09:42 25 4
gpt4 key购买 nike

下面给出的是我在数据数组中的数据。我在下面的代码中所做的是,根据给定的数据,我必须以特殊格式构造 json,我也在下面给出了这种格式。

//code start here
var hierarchy={};
hierarchy.name="Hierarchy";
hierarchy.children=[{"name":"","children":[{"name":"","children":[]}]}];
var countryindex;
var flagExist=false;
var data = [
{country :"America", city:"Kansas", employe:'Jacob'},
{country :"Pakistan", city:"Lahore", employe:'tahir'},
{country :"Pakistan", city:"Islamabad", employe:'fakhar'} ,
{country :"Pakistan", city:"Lahore", employe:'bilal'},
{country :"India", city:"d", employe:'ali'} ,
{country :"Pakistan", city:"Karachi", employe:'eden'},
{country :"America", city:"Kansas", employe:'Jeen'} ,
{country :"India", city:"Banglore", employe:'PP'} ,
{country :"India", city:"Banglore", employe:'JJ'} ,

];


for(var i=0;i<data.length;i++)
{


for(var j=0;j<hierarchy.children.length;j++)
{
//for checking country match
if(hierarchy.children[j].name==data[i].country)
{
countryindex=j;
flagExist=true;
break;

}

}

if(flagExist)//country match now no need to add new country just add city in it
{
var cityindex;
var cityflag=false;
//hierarchy.children[countryindex].children.push({"name":data[i].city,"children":[]})
//if(hierarchy.children[index].children!=undefined)
for(var k=0;k< hierarchy.children[countryindex].children.length;k++)
{
//for checking city match
if(hierarchy.children[countryindex].children[k].name==data[i].city)
{
// hierarchy.children[countryindex].children[k].children.push({"name":data[i].employe})
cityflag=true;
cityindex=k;
break;
}

}
if(cityflag)//city match now add just empolye at that city index
{
hierarchy.children[countryindex].children[cityindex].children.push({"name":data[i].employe});

cityflag=false;
}
else//no city match so add new with employe also as this is new city so its emplye will be 1st
{
hierarchy.children[countryindex].children.push({"name":data[i].city,children:[{"name":data[i].employe}]});
//same as above
//hierarchy.children[countryindex].children[length-1].children.push({"name":data[i].employe});


}
flagExist=false;
}
else{ //no country match adding new country //with city also as this is new city of new country
console.log("sparta");
hierarchy.children.push({"name":data[i].country,"children":[{"name":data[i].city,"children":[{"name":data[i].employe}]}]});

// hierarchy.children.children.push({"name":data[i].city,"children":[]});
}


//console.log(hierarchy);
}
hierarchy.children.shift();
var j=JSON.stringify(hierarchy);

//code ends here

//这是我从代码中成功形成的json

{

"name":"Hierarchy",
"children":[
{
"name":"America",
"children":[
{
"name":"Kansas",
"children":[{"name":"Jacob"},{"name":"Jeen"}]}]},
{
"name":"Pakistan",
"children":[
{
"name":"Lahore",
"children":
[
{"name":"tahir"},{"name":"bilal"}]},
{
"name":"Islamabad",
"children":[{"name":"fakhar"}]},
{
"name":"Karachi",
"children":[{"name":"eden"}]}]},
{
"name":"India",
"children":
[
{
"name":"d",
"children":
[
{"name":"ali"}]},
{
"name":"Banglore",
"children":[{"name":"PP"},{"name":"JJ"}]}]}]}

现在最初的问题是,目前我正在解决三个键数组的数据的问题,我必须进行 3 个嵌套循环,现在我想优化这个解决方案,以便如果对象的数据数组有超过 3 个键说5 {国家:“美国”,州:“纽约”,城市:“newYOrk”,街道:“elm”,员工:'雅各布'},或者超过我的解决方案将不起作用,并且我无法在出现多少个键之前决定,所以我认为递归可能最适合这里。但我写递归很糟糕,而且情况也很复杂。一些很棒的程序员可以帮助我编写递归或建议其他解决方案吗?

最佳答案

我做了一个可以帮助你的函数,是递归的并且支持任何长度级别,但是代码有点长

  var data = [
{country :"America", city:"Kansas", employe:'Jacob'},
{country :"Pakistan", city:"Lahore", employe:'tahir'},
{country :"Pakistan", city:"Islamabad", employe:'fakhar'} ,
{country :"Pakistan", city:"Lahore", employe:'bilal'},
{country :"India", city:"d", employe:'ali'} ,
{country :"Pakistan", city:"Karachi", employe:'eden'},
{country :"America", city:"Kansas", employe:'Jeen'} ,
{country :"India", city:"Banglore", employe:'PP'} ,
{country :"India", city:"Banglore", employe:'JJ'} ,

];


function groupByLevel( source, levels, config ){
config = ( typeof config == "object" && config ) || {};

var internalConfig = {
keyName: config.keyName || "name",
collectionName: config.collectionName || "children"
};

var hierarchy = {};
hierarchy[internalConfig.keyName] = "Hierarchy";
hierarchy[internalConfig.collectionName] = source;

var sourceArray = [hierarchy];

for(var i = 0; i < levels.length; i++){

var newSourceArray = [];

for(var j = 0; j < sourceArray.length; j++){

var oldArray = sourceArray[j][internalConfig.collectionName];
var newArray = groupBy(oldArray, levels[i], internalConfig);
sourceArray[j][internalConfig.collectionName] = newArray;
for(var l = 0; l < newArray.length; l++){
newSourceArray.push(newArray[l]);
}
}
sourceArray = newSourceArray;
}
return hierarchy;
}


function duplicate(obj){
var newObj = {};
for(var i in obj) newObj[i] = obj[i];
return newObj;
}

function objlength(obj){
var length = 0;
for(var i in obj) length++;
return length
}

function groupBy(array, key, config){
var group = {};

for(var i = 0; i < array.length; i++){

var item = array[i];
var currentKey = item[key];

if( ! group[currentKey] ) group[currentKey] = [];

var clone = duplicate(item);
delete clone[key];
if( objlength(clone) ) group[currentKey].push( clone );

}
var returnGroup = [];
for(var i in group){
var groupItem = {};
groupItem[config.keyName] = i;
if( group[i].length ) groupItem[config.collectionName] = group[i];
returnGroup.push( groupItem );
}
return returnGroup;
}


var result = groupByLevel(data, ["country", "city", "employe"] );
var yourJson = JSON.stringify(result);

函数 groupByLevel、groupBy、duplicate 和 objLength 将处理您的分组。

在此链接中http://jsfiddle.net/Castrolol/2TkkW/您可以使用您的数据查看更高级的示例。

如果需要,您可以放置​​单个对象来分离并可重复使用。

抱歉我的英语不好。

关于javascript 复杂递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19811040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com