0

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
    });

}
1
  • 1
    Replace item.FormID with data[i].FormID. Same for FormName, or create a variable called item and assign it with data[i] before the push call in the loop Commented Aug 24, 2014 at 3:54

5 Answers 5

2

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
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

In jQuery, item is the second argument
1

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
     });

}

2 Comments

Thankyou. both of your codes are working, i tried both.. But which one should i go for. I mean should i go for jquery or simple javascript??
$.each needs to include jquery library in your HTML, and if you don't want to include jquery library then go with simple javascript for loop for(var i=0;i<...
1
i < data

Should be

i < data.length

Assuming data is an array

Comments

1

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) { ... }));

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.