将元素排列到一个散列数组中 [英] arranging elements in to a hash array
问题描述
我试图将一个JavaScript对象分解为小数组,以便我可以随时轻松访问内层数据。
我使用递归函数访问json中的所有节点,使用该程序
我在这里要做的是我想将它们存储到一个单独的数组中,这样我cn访问它像
newArray.Microsoft =微软的MSFT;
newArray.Intel Corp =(INTC,Fortune 500);
newArray.Japan =日本
newArray.Bernanke =伯南克;
每个数组的深度都不相同,所以具有单个级别的数组可以使用相同的名称,例如I ve在示例伯南克中显示。是否可以这样做?
不,您可以减少 Facets
添加到一个名为 html
的字符串中 - 但是您想要一个对象。
function generateList(facets){
var map = {};
(函数recurse(arr){
var join = [];
for(var i = 0; i< arr.length; i ++){
var current = arr [ i] .term; //每个对象必须有一个!
current = current.replace(// g,_);
join.push(current); //只在最低级别上。
if(current in arr [i])
map [current] = recurse(arr [i] [current]);
}
return join;
} )(小平面)
返回地图;
}
要获得单层数据,您可以在添加else语句之后if:
else
map [current] = [current]; //手动创建数组
尽管我不认为结果( demo )非常有意义。
I am trying to break a javascript object in to small array so that I can easily access the innerlevel data whenever I needed.
I have used recursive function to access all nodes inside json, using the program
What I am trying to do here is that I want to store these in to a separate array so that I cn access it like
newArray.Microsoft= MSFT, Microsoft;
newArray.Intel Corp=(INTC, Fortune 500);
newArray.Japan=Japan
newArray.Bernanke=Bernanke;
Depth of each array are different, so the ones with single level can use the same name like I ve shown in the example Bernanke. Is it possible to do it this way?
No, you reduce the Facets
to a string named html
- but you want an object.
function generateList(facets) {
var map = {};
(function recurse(arr) {
var join = [];
for (var i=0; i<arr.length; i++) {
var current = arr[i].term; // every object must have one!
current = current.replace(/ /g, "_");
join.push(current); // only on lowest level?
if (current in arr[i])
map[current] = recurse(arr[i][current]);
}
return join;
})(facets)
return map;
}
To get the one-level-data, you could just add this else-statement after the if:
else
map[current] = [ current ]; // create Array manually
Altough I don't think the result (demo) makes much sense then.
这篇关于将元素排列到一个散列数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!