Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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_Data Structures_Treeview - Fatal编程技术网

Javascript 字符串数组到树数据结构

Javascript 字符串数组到树数据结构,javascript,data-structures,treeview,Javascript,Data Structures,Treeview,从服务器返回的数据包含如下层次结构的字符串数组: [ "house.bedroom.bed", "house.kitchen.spoon", "house.kitchen.knife", "house.bedroom.sofa", "house.bedroom.tv", "plants.trees", "house.birds.parrot.grey" ...] root house bedroom bed sofa tv

从服务器返回的数据包含如下层次结构的字符串数组:

[
 "house.bedroom.bed",
 "house.kitchen.spoon",
 "house.kitchen.knife",
 "house.bedroom.sofa",
 "house.bedroom.tv",
 "plants.trees",
 "house.birds.parrot.grey"
 ...]
root
  house
    bedroom
      bed
      sofa
      tv
    kitchen
      spoon
      knife
    birds
      parrot
        grey
  plants
    trees
如何从中创建树数据结构,以便以树的形式输出数据

像这样:

[
 "house.bedroom.bed",
 "house.kitchen.spoon",
 "house.kitchen.knife",
 "house.bedroom.sofa",
 "house.bedroom.tv",
 "plants.trees",
 "house.birds.parrot.grey"
 ...]
root
  house
    bedroom
      bed
      sofa
      tv
    kitchen
      spoon
      knife
    birds
      parrot
        grey
  plants
    trees
最简单的方法是什么

有没有办法扭转这种局面?例如,被问到“刀”我想返回“房子.厨房.刀”


提前感谢

这里有一种方法,可能不是最有效的,但很有效。如果不希望叶作为空对象,可以根据需要对其进行修改

var r=[
 "house.bedroom.bed",
 "house.kitchen.spoon",
 "house.kitchen.knife",
 "house.bedroom.sofa",
 "house.bedroom.tv",
 "plants.trees",
 "house.birds.parrot.grey"];

var o={}; // output object
function build(o,p){
 p.split(".").forEach(function(d){
   o = o[d] || (o[d]={});  
 });
}    

r.forEach(function(a,i){ // build up each branch based on path
 build(o, a);
});
o

可以采用嵌套数组的数组,其中第一个元素是名称

对于查找所需字符串,它使用递归方法,保留实际元素的路径,以便以后连接所需字符串

。。。对,为什么是数组而不是时髦的对象?很高兴你这么问。数组允许维护特定的顺序,而不依赖于有序对象的实际实现

函数查找([key,values],string,temp=[])){
var结果;
温度=温度混凝土(键);
如果(键===字符串){
返回临时切片(1).join('.');
}
一些(a=>result=find(a,string,temp));
返回结果;
}
var数组=[“房子.卧室.床”,“房子.厨房.勺子”,“房子.厨房.刀”,“房子.卧室.沙发”,“房子.卧室.电视”,“植物.树木”,“房子.鸟.鹦鹉.灰色”],
结果=数组。减少((r,s)=>{
('root.+s).拆分('.')。减少((a,项)=>{
变量数组=a.find(([v])=>v==item);
if(!数组){
a、 推送(数组=[item,[]);
}
返回数组[1];
},r);
返回r;
},[]).pop();
console.log(查找(结果“刀”);//房子、厨房、小刀
console.log(查找(结果'42'));//没有定义,还有什么?
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
下面的代码将为您提供包含关键字的最后一个字符串。如果这不是你想要的,请告诉我

让stringsArray=[
“房子,卧室,床”,
“房子,厨房,勺子”,
“房子,厨房,刀”,
“房子、卧室、沙发”,
“房子、卧室、电视”,
“植物,树木”,
“房子,鸟,鹦鹉,灰色”
];
函数findInArray(){
let关键字=document.getElementById('search')。值;
让results=document.getElementById('results');
forEach(函数(字符串,索引){
if(string.includes(关键字)){
results.innerHTML=string;
}
});
}
搜索:
搜寻
const数据=[
“房子,卧室,床”,
“房子,厨房,勺子”,
“房子,厨房,刀”,
“房子、卧室、沙发”,
“房子、卧室、电视”,
“植物,树木”,
“房子,鸟,鹦鹉,灰色”
];
const mainMapFromStart={};
const mainMapFromEnd={};
功能集(部件、主地图){
设map=mainMap;
用于(零件的常数项){
地图[项目]=地图[项目]| |{};
地图=地图[项目];
}
}
data.map(item=>item.split('.')).forEach(parts=>{
设置(零件,主地图从开始);
设置(parts.reverse(),mainMapFromEnd);
});
log(JSON.stringify(mainMapFromStart,null,4));

log(JSON.stringify(mainMapFromEnd,null,4))请将想要的结构添加为javascript数组/对象?还有你尝试过的。@NinaScholz我还没有尝试过任何东西,因为我用字符串找不到类似的答案,我试图把(.)分开,但我失败了。效果很好。有什么办法可以扭转它吗?例如,我想把刀子还给house.kitchen.knife,刀子工作得很好,有没有办法把它倒过来?例如,我想把刀子还给house.kitchen.knife,刀子工作得很好,有没有办法把它倒过来?例如,我想返回house.kitchen.knifeSure,为此,您还应该使用反向地图。编辑了答案。