How to filter the data from array using javascript/jquery?

jquery filter array multiple values
jquery filter array of objects
javascript array.filter multiple arguments
filter array of objects javascript
jquery filter json array by value
jquery find in array of objects
javascript filter object
jquery filter list

I have declared an array as var user_profile = []; and I have already some pushed data in array which contains :

[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

I only need the data in array whose assessCatId=1,2,3 and assessReason is not Empty . So I tried by filtering it:

const output = user_profile.filter(({ assessCatId,assessReason })  => assessCatId != 0 || assessReason != 'empty');
            console.log("here the outpuit is");
            console.log(output)  ;

But it is not filtering the array.Still every data is coming in the array.How can i manage this array to filter the data?


You can insert any value in array and check it

let k=[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

let arr=[1,2,3]
let d=k.filter(a=>arr.indexOf(a.assessCatId) > -1 && a.assessReason!="" && a.assessReason!=null)
console.log(d)

jQuery.grep(), You may use jQuery.grep() : var found_names = $.grep(names, function(v) { return v.name === "Joe" && v.age < 30; });. DEMO: http://jsfiddle.net/ejPV4/. The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not execute the function for array elements without values. Note: filter() does not change the original array.


You have to use && instead of ||. Try

(assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null)

var user_profile =[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

const output = user_profile.filter(({ assessCatId,assessReason })  => (assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null));
console.log("here the outpuit is");
console.log(output)  ;

javascript filter array of objects, Return an array of all the values in the ages array that are 18 or over: var ages The filter() method creates an array filled with all array elements that pass a test  The Array.prototype.filter () function is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument function.


var arr = [{ "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "2", "assessAmount": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "2", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": null, "assessAmount": "", "assessPenalty": "", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": null, "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-09", "assessAmount": "2075-09-17", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-09", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-17", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 1, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 2, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 3, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 1, "assessReason": "A", "assessAmount": "1", "assessPenalty": "2", "assessTotal": 3 }, { "assessmentType": "FINAL", "assessCatId": 2, "assessReason": "B", "assessAmount": "3", "assessPenalty": "4", "assessTotal": 7 }, { "assessmentType": "FINAL", "assessCatId": 3, "assessReason": "C", "assessAmount": "5", "assessPenalty": "6", "assessTotal": 11 }]
var o = arr.filter((f) => [1, 2, 3].indexOf(f.assessCatId) != -1 && f.assessReason)
console.log(o)

JavaScript Array filter() Method, How to Create Dark/Light Mode for Website using JavaScript/jQuery? JavaScript | Namespace. JavaScript Array filter() Method. Below is the example of  The filter () method creates a new array with all the elements that pass the test implemented by the callback () function. Internally, the filter () method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array.


Just replace by ({ assessCatId,assessReason })=>assessCatId>0 && assessReason!='')

var user_profile = [  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
];


const output = user_profile.filter(({ assessCatId,assessReason })=>assessCatId>0 && assessReason!='');
  
  console.log("output", output.length, output);

JavaScript Array filter() Method, $(selector).filter(criteria, function(index)) jQuery code to show the working of the filter() method jquery/3.3.1/jquery.min.js"></ script > between lodash and Underscore · How to Create a Dropdown List with Array Values using JavaScript ? Definition and Usage. The filter() method returns elements that match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned.


var output = [];
for(var i=0; i < user_profile.length; i++){
    var assessCatId = user_profile[i].assessCatId;
    var assessReason = user_profile[i].assessReason;
    if((assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null)){
        output.push(user_profile[i]);
    }
}
console.log(output);

jQuery, This tutorial shows you how to use the JavaScript array filter method to filter call the filter() methods of the data array and pass in the isInRange() function and  filter(json, function(element) { if(element.price <= 1000 && element.sqft >= 500 && element.num_of_beds > 2 && element.num_of_baths > 2.5) return true; return false; }); This way, you can invoke the filter based on whatever predicate you define, or even filter multiple times using smaller filters. share.


JavaScript Array Filter: Filtering Array Elements Based on a Test , To filter object array based on attributes in jQuery, use the map() src="https://​ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


How to filter object array based on attributes in jQuery?, filter() calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback  The test is a function that is passed an array item and the index of the item within the array. Only if the test returns true will the item be in the result array. The filter function will be passed two arguments: the current array item and its index. The filter function must return 'true' to include the item in the result array.


Array.prototype.filter(), In this blog, read about the easiest way to find the element/object in a JavaScript array which satisfy the filter condition. Know more. Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John: