开发者

Pushing a composite array element

开发者 https://www.devze.com 2023-03-28 00:30 出处:网络
Consider the following \'push\' scenario. (global) var refs = [] ; var record = {id:1, references: [\"12\",\"145\"]};

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"]};
0

精彩评论

暂无评论...
验证码 换一张
取 消