1

My code is:

 colModel: [

    { display: 'Grade', name: 'Grade', width: 40, align: 'center' },
    { display: 'Grade ID', name: 'ID', width: 180, align: 'left' },
    { display: 'Organization ID', name: 'Organization_ID', width: 120, align: 'left' },
    { display: 'Organization Name', name: 'Organization_Name', width: 130, align: 'left', hide: true }
    ]

This is passed as a object to a function.

i want to filter it like

// This worked

   alert(colModel[0].display);

   var obj = colModel.filter(function () {
               return $(this).name == "Grade"; });

  alert(obj.display);

But this come to be undefined.

ANy help is appreciated

Edit:

The object is passed as a option to a plugin:

$('#filteredResult').FilteredPagedTable({ url: "http://localhost:2014/mySer.svc/GetFilteredPaged", grid: true, pageSize: 4, pageNo: 0, columnName: "ID", IsAscending: true, filteredColumn: $('#ddlColumns').val(), SearchedValue: $('#txtSearchTextBox').val() ,

    colModel: [

    { display: 'Grade', name: 'Grade', width: 40, align: 'center' },
    { display: 'Grade ID', name: 'ID', width: 180, align: 'left' },
    { display: 'Organization ID', name: 'Organization_ID', width: 120, align: 'left' },
    { display: 'Organization Name', name: 'Organization_Name', width: 130, align: 'left', hide: true }
    ]

 });
Moons
  • 3,833
  • 4
  • 49
  • 82
  • `filter()` returns a collection of object even if there´s only one object matching you filter criteria. – Stefan Dec 09 '11 at 14:29
  • What is $(colsToDisplay)? It doesn´t seem to be the "colModel". – Stefan Dec 09 '11 at 14:32
  • Try `alert(obj[0].display);` just after your filtering. It should select the first matched object in the collection. Notice that it will fail if there´re no matched element. – Stefan Dec 09 '11 at 14:39
  • @Stefan Actually i am testing my application in IE it is still throwing an Object does not support this property but in other browsers it is working with obj[0].display – Moons Dec 09 '11 at 14:45
  • @Stefan See http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes the accepted answer says it is a part of ECMASCRIPT 5 plus additional code for IE but i dont know where to place that code – Moons Dec 09 '11 at 14:46
  • I tested this in a JSFiddle and can reliably get the result back into `obj` - but when I try and access a property - I get undefined. Check it out with a debug console open here: http://jsfiddle.net/Sohnee/N6W7r/ – Fenton Dec 09 '11 at 15:00
  • Yeah, but why create prototype functions for IE when you´re already using jQuery? – Stefan Dec 09 '11 at 15:01
  • I have updated the JSFiddle as I got it working - your original question is almost right, except for one small detail. – Fenton Dec 09 '11 at 15:18

3 Answers3

1
var matchedCols = {};
$.each(colModel, function(i) {
    if (this.name == "Grade") {
      matchedCols[i] = this;
    }
});

See http://jsfiddle.net/acVbZ/

Stefan
  • 5,644
  • 4
  • 24
  • 31
1

I've worked this one out...

Where you have

alert(obj.display);

You should use

alert(obj[0].display);

As filter returns an array of matches - even when only one result is found.

See it working here: http://jsfiddle.net/Sohnee/N6W7r/

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

Why using jQuery?

var colModelFiltered = colModel.filter(function(element, index, array){
 return element.name === "Grade";
});

EDIT (by Kamal Deep Singh)

You also need to put:

 if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/) {
  var len = this.length >>> 0;
   if (typeof fun != "function")
   throw new TypeError();

var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
  if (i in this) {
    var val = this[i]; // in case fun mutates this
    if (fun.call(thisp, val, i, this))
    res.push(val);
  }
}
return res;
};
}

in the document.ready() event to make it work on IE too.

More details are on:
Javascript: How to filter object array based on attributes?

Community
  • 1
  • 1
Aron Woost
  • 19,268
  • 13
  • 43
  • 51