Pushing a composite array element
Consider the following 'push' scenario.
(global) var refs = [] ;
var record = {id:1, references: ["12","145"]};
refs.push(record);
Nevertheless, when I debug with Chrome, the push function is skipped and the refs array is empty at the end. What is the reason?
EDIT:
The full code is something like this:
gadgets.sciverse.makeRequest(entry['prism:url'], function(obj) {
var testJson = $.xml2json(obj['text']);
// console.log(testJson);
var tempArr = [];
if (!testJson) {
console.log('empty secondary response');开发者_Go百科
return ;
}
var refSet = testJson ['item']['bibrecord']['tail']['bibliography']['reference'];
if (!refSet)
tempArr = [] ;
else {
$.each(refSet, function(i, e){
tempArr.push(e['ref_info']['refd_itemidlist']['itemid']['text']);
})
}
var verySillyTemp = {
id: pid,
arr: tempArr
} ;
refs.push( verySillyTemp );
},params);
console.log(refs);
Looks like refs
is being set in an asynchronous call and you are logging it before it is set.
Consider using an alternative primitive operation over function calls in performance critical loops and functions.
You can use the following method.
record[record.length] = id;
It's just a dummy example, you can run the for
loop through you record array.
Primitive operations can be faster than function calls.
Syntax error:
var record = {id:1, references: ["12","145"]};
精彩评论