admin管理员组文章数量:1406063
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
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
Share Improve this question edited Jul 24, 2015 at 5:36 Jeff Mercado 135k33 gold badges266 silver badges280 bronze badges asked Jul 10, 2015 at 11:35 stigstig 1,2406 gold badges26 silver badges45 bronze badges 4- 2 are you iterating upon an array and getting something from each element ? Better to post a sample question like How to concatinate words in array ? Syntax from other languages are just confusing. – Mritunjay Commented Jul 10, 2015 at 11:36
- In the javascript, its an array inside a object – stig Commented Jul 10, 2015 at 11:37
- hi please find this URL that may be help to you stackoverflow./questions/11887450/… – Neo Vijay Commented Jul 10, 2015 at 11:43
- a sample of your source data would go a long way to getting help on how to do something with it - I take it, since you don't want any libraries (except the biggest piece of bloat in existence, jQueery) linqjs.codeplex. is out of the question – Jaromanda X Commented Jul 10, 2015 at 11:44
2 Answers
Reset to default 3You 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);
});
Second one is easy enough to do in vanilla JavaScript:
var a = "";
for (var i = 0; i < DbList2.length; i++){
a += DbList2[i].FieldB + ", ";
}
First one is a little trickier, but not impossible and can also be done with vanilla JS.
var a = "";
var uniques = [];
for (var i = 0; i < DbList1.length; i++ ){
var fieldA = DbList1[i].FieldA;
// check if we've already seen this value
if (uniques.indexOf(fieldA) < 0)
{
// Nope, record it for future use
uniques.push(fieldA)
// and update the string.
a += fieldA + ", ";
}
}
本文标签: LinQ foreach in javascriptjqueryStack Overflow
版权声明:本文标题:LinQ foreach in javascriptjquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744959988a2634594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论