StrugglingCoder StrugglingCoder - 4 months ago 9
AngularJS Question

Filter in JavaScript not working

I have an array of elements in Javascript and I want to filter based on it's properties.

Now , the response object which I want to filter looks something like:

enter image description here

And the individual objects may have

row_stat_cd
either
"I"
or
"L"
.

I want to get only the
"I"
records. So I did ..

referenceDataRecord.filter(isInactive);

function isInactive(obj) {
return obj.row_stat_cd != "L";
};

angular.forEach(referenceDataRecord, function (k, v) {
.........


but still referenceDataRecord contains all the records . It is not being filtered and keeping only the
"I"
records.

Answer

The filter method by itself does not modify the array. You have to set reference to the variable

referenceDataRecord = referenceDataRecord.filter(isInactive);

    function isInactive(obj) {
        return obj.row_stat_cd != "L";
    };

    angular.forEach(referenceDataRecord, function (k, v) {

The filter() method creates a new array with all elements that pass the test implemented by the provided function

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Comments