gpt4 book ai didi

javascript - javascript中关联键和数字的数组组合

转载 作者:行者123 更新时间:2023-11-28 13:47:28 25 4
gpt4 key购买 nike

简而言之,我已经开始我的问题了,

我只是读取json文件,

 [{"FirstCategory":"Bath","SecondCategory":"Bath Accessories","ThirdCategory":""}, {"FirstCategory":"Bath","SecondCategory":"Faucets","ThirdCategory":""},{"FirstCategory":"Bath","SecondCategory":"Fixtures","ThirdCategory":""},{"FirstCategory":"Bath","SecondCategory":"Vanities","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Concrete","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Fencing","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Gypsum","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Insulation","ThirdCategory":""},{"FirstCategory":"Building Materials","SecondCategory":"Insulssdation","ThirdCategory":""}]

并尝试转换为数组,例如

Array
(
[Bath] => Array
(
[Bath Accessories] => Array
(
[0] => test
)

[Faucets] => Array
(
[0] => test1
[1] => test2
)

)

)//sorry i have used PHP for simple formatting the array.

我花了很多时间在这件事上,但我无法取得成功,请帮助我。

 My javascript code : (not working.)

var FirstCategory = [];
var SecondCategory = [];
var ThirdCategory = [];


$.getJSON('tree.json', function(data) {

var dataObj = new Array();
$.each(data,function(i){
dataObj[data[i].FirstCategory] = new Array();

if([data[i].SecondCategory] in dataObj[data[i].FirstCategory])
dataObj[data[i].FirstCategory][data[i].SecondCategory] = data[i].SecondCategory;
else
dataObj[data[i].FirstCategory][data[i].SecondCategory] = new Array();

dataObj[data[i].FirstCategory][data[i].SecondCategory][data[i].ThirdCategory] = new Array();

});

console.log(dataObj);

/*
$.each(data,function(i){

if (FirstCategory == '') {
FirstCategory.push(data[i].FirstCategory);
}
else
{
if(!FirstCategory.contains(data[i].FirstCategory))
{
//root
FirstCategory.push(data[i].FirstCategory);
}
else
{
//------- second level category -------//
if (SecondCategory == '') {
SecondCategory.push(data[i].SecondCategory);
}
else
{
if(!SecondCategory.contains(data[i].SecondCategory))
{
SecondCategory.push(data[i].SecondCategory);
}
else
{
ThirdCategory.push(data[i].ThirdCategory);
}
}

}
}

});
*/

});


Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}

提前致谢。

最佳答案

请注意Javascript has no "associative arrays" .

更加编程化:

var levels = ["FirstCategory", "SecondCategory", "ThirdCategory"]; // possibly more
var dataObj = {}; // an empty object
for (var i=0; i<data.length; i++) {
var cur = dataObj;
for (var j=0; j<levels.length; j++) {
var key = data[i][levels[j]];
if (!key) // empty
break;
if (key in cur)
cur = cur[key];
else
cur = cur[key] = {};
}
}

示例输入的结果 (dataObj),格式为 JSON:

{
"Bath": {
"Bath Accessories": {},
"Faucets": {},
"Fixtures": {},
"Vanities": {}
},
"Building Materials": {
"Concrete": {},
"Fencing": {},
"Gypsum": {},
"Insulation": {},
"Insulssdation": {}
}
}

关于javascript - javascript中关联键和数字的数组组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13196106/

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