我想用JSON發送一個對象,我用for循環迭代這個對象。我發送的JSON是正確的,但問題是數組中存在空值。我認爲它與myValue.children[index]= item;
我試過.filter()方法,但它沒有解決我的問題,因爲返回的數組是空的。我也試過一個條件,但我只有4個值被檢測到。 這是我收到的JSON:刪除陣列上的空值
{
"name":"object",
"children":[
{
"name":"myName",
"children":[
{
"name":"aNumber",
"size":12345
}
]
},
null,
null,
null,
{
"name":"myName",
"children":[
{
"name":"aNumber",
"size":12345
}
]
},
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"myName",
"children":[
{
"name":"aNumber",
"size":12345
}
]
},
{
"name":"myName",
"children":[
{
"name":"aNumber",
"size":12345
}
]
}
]
}
這是我的代碼:
function myFunction(index, tabItem , callback){
var result = '';
var myValue = {};
var childrenTicket = [];
myValue.name = "object";
myValue.children = childrenTicket;
for (index in tabItem) {
try{
var currentItem = tabItem[index];
var Object =currentItem.issuelinks.issuelinktype.outwardlinks.issuelink;
for(var SeveralObject in Object) {
var item = {};
var specificObject = Object[SeveralObject];
var main = specificObject._;
var id = specificObject.$.id;
var descriptionObject = {};
descriptionObject.name = id;
descriptionObject.size = 12345;
item.name = main;
if (myValue.children[index] == undefined){
console.log('undefined detected');
}
myValue.children[index]= item;
//result = result + (myValue);
item.children = [descriptionObject];
sendJson(myValue);
callback(result);
}
} catch(ignored){
}
} return result;
}
希望你能幫助我。
我已經試過'itemTicket.children [index]'但它不起作用,你認爲我用它在好物體上嗎? – Anonyme
您需要傳遞整個JSON對象作爲'obj'。 – 2016-04-21 07:03:25
我在我的JSON文件上使用它,但結果是完全一樣的(對於itemTicket中的變量){ if(itemTicket [i] === null || itemTicket [i] === undefined){ delete itemTicket [一世]; (itemTicket);' – Anonyme