2

I have created array like below,

arr =[ {uName:"2015", 1:100,2:10,3:0},{uName:"2011",1:10,2:2,3:19},{uName:"2015",1:10,2:0,3:20}],

Here, I need to select arr[0][1] and arr[2][1] where uName is "2015".

Linq query is,

$.Enumerable.From(arr).Where(function(n){n.uName =="2015"}).Select(function(y){return y.1}).Sum()

Expected answer is:

arr[0][1] = 100, arr[2][1] = 10 (or) sum of these value 110 (arr[0][1] + arr[2][1])
prabu_s
  • 137
  • 1
  • 6

1 Answers1

1

You need to restructure your array, because that's not a valid javascript object. You cannot mix an object structure with an array structure.

var arr =[ {uName:"2015", num1: 100,num2: 10, num3: 0},
           {uName:"2011", num1: 10, num2: 2, num3: 19},
           {uName:"2015",num1: 10, num2: 0, num3: 20}];

var expected = $.Enumerable.From(arr).Where(
               function(x){return x.uName =="2015"}).Select(
               function(a){ return a.num1}).Sum();

Here is the fiddle: http://jsfiddle.net/99eu7/37/

Mario Murrent
  • 712
  • 5
  • 24