简体   繁体   中英

jquery for loop instead of underscore.js _.each

ok, well i am not good with looping so here it goes..

i want to for loop this _.each

 _.each(data, function (item) {
                            newData.push({
                                id: item.FormID,  //id part present in data
                                text: item.FormName  //string to be displayed
                            });
                        });

_.each is a function of underscore.js library.

i don't want to use the library, just the plain jquery or javascript.

i tried like this.. i know its wrong. but have tried and failed.

for ( var i = 0; i < data; i++ ) {
    newData.push({
        id: item.FormID,  //id part present in data
        text: item.FormName  //string to be displayed
    });

}

It is same in jQuery,

$.each(data, function(index, item) {
    newData.push({
        id: item.FormID, //id part present in data
        text: item.FormName //string to be displayed
    });
});
i < data

Should be

i < data.length

Assuming data is an array

do something like this:

$.each(data, function (index,value) {
                        newData.push({
                            id: value.FormID,  //id part present in data
                            text: value.FormName  //string to be displayed
                        });
                    });

you can also use for loop like this:

for ( var i = 0; i < data.length; i++ ) {
    newData.push({
       id: data[i].FormID,  //id part present in data
       text: data[i].FormName  //string to be displayed
     });

}

JavaScript arrays have forEach methods of their own:

data.forEach(function(item) {
    newData.push({ id: item.FormId, text: item.FormName });
});

Or, since you seem to be transforming the data array into another array, you could use map :

var newData = data.map(function(item) {
    return {
        id: item.FormId,
        text: item.FormName
    };
});

If newData already has things in it, then you could use concat to append the results from map :

var newData = [ ... ];
newData = newData.concat(data.map(function(item) {
    return {
        id: item.FormId,
        text: item.FormName
    };
}));

You could even use push and apply :

newData.push.apply(newData, data.map(function(item) { ... }));

jQuery is not what I call plain , it's already a library by itself, and it's a library in which the arguments of the iterator for each are inverted, compared to the plain javascript .

Unless you care about IE8, I suggest you to use Array.prototype.forEach which is available in plain javascript :

data.forEach(function (item) {
    newData.push({
        id: item.FormID,  //id part present in data
        text: item.FormName  //string to be displayed
    });
});

(And if you care about IE8, you can use the polyfill from MDN )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM