Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinQ foreach in javascript/jquery

How can I easily do these type of loops in Jquery or Javascript? Preferably without any other plugins.

string a = "";
foreach (var i in (from a in DbList1 select a.FieldA).Distinct())
{
   a += i + ", ";
}

and this

foreach (var i in DbList2)
{
   a += i.FieldB + ", ";
}

Loop number 2 could be solved like this atleast.

$.each(aData.DbList2, function (index, value) {
 a += value.FieldB;
);

Not 100% sure this is the most effective though

like image 900
stibay Avatar asked Jul 10 '15 11:07

stibay


1 Answers

You can use map method for iterating array variable.

Code snippets:

var arr = jQuery.map( aData.DbList2, function(value) {
return value.FieldB;
});
//To get unique array variable
var uniqueArr = [];
$.each(arr, function (i, el) {
            if ($.inArray(el, uniqueArr) === -1) uniqueArr.push(el);
        });
like image 61
John R Avatar answered Sep 30 '22 16:09

John R