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 }
    ]

 });
8
  • filter() returns a collection of object even if there´s only one object matching you filter criteria. Commented Dec 9, 2011 at 14:29
  • What is $(colsToDisplay)? It doesn´t seem to be the "colModel". Commented Dec 9, 2011 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. Commented Dec 9, 2011 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 Commented Dec 9, 2011 at 14:45
  • @Stefan See stackoverflow.com/questions/2722159/… 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 Commented Dec 9, 2011 at 14:46

3 Answers 3

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

See http://jsfiddle.net/acVbZ/

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

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?

3 Comments

this does not solves my problem. i have also updated my question
Why not filtering the array according to my solution before passing to the plugin?
Your code is throwing an error of Object does not support this property or method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.