Array of javascript filters

I have an array of objects, and I wonder how best to look for it. Given the example below, how can I search for name = "Joe" and age < 30 ? Is there anything jQuery can help me or make me do this search on my own?

 var names = new Array(); var object = { name : "Joe", age:20, email: "joe@hotmail.com"}; names.push(object); object = { name : "Mike", age:50, email: "mike@hotmail.com"}; names.push(object); object = { name : "Joe", age:45, email: "mike@hotmail.com"}; names.push(object); 
+74
javascript jquery arrays
Nov 27 '12 at 23:02
source share
7 answers

You can use jQuery.grep() :

 var found_names = $.grep(names, function(v) { return v.name === "Joe" && v.age < 30; }); 

DEMO: http://jsfiddle.net/ejPV4/

+113
Nov 27
source share

You can do this very easily with the [].filter :

 var filterednames = names.filter(function(obj) { return (obj.name === "Joe") && (obj.age < 30); }); 

You will need to add padding for browsers that do not support the [].filter : this MDN page gives this code.

+71
Nov 27 '12 at 23:05
source share

You can use the jQuery.filter () function to return elements from a subset of the corresponding elements.

 var names = [ { name : "Joe", age:20, email: "joe@hotmail.com"}, { name : "Mike", age:50, email: "mike@hotmail.com"}, { name : "Joe", age:45, email: "mike@hotmail.com"} ]; var filteredNames = $(names).filter(function( idx ) { return names[idx].name === "Joe" && names[idx].age < 30; }); $(filteredNames).each(function(){ $('#output').append(this.name); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="output"/> 
+7
Sep 26 '14 at 7:50
source share

 var nameList = [ {name:'x', age:20, email:'x@email.com'}, {name:'y', age:60, email:'y@email.com'}, {name:'Joe', age:22, email:'joe@email.com'}, {name:'Abc', age:40, email:'abc@email.com'} ]; var filteredValue = nameList.filter(function (item) { return item.name == "Joe" && item.age < 30; }); //To See Output Result as Array console.log(JSON.stringify(filteredValue)); 

You can just use JavaScript :)

+4
Jan 24 '17 at 15:46
source share

Such a quick question. What if you have two arrays of objects and you want to "align" these arrays of objects so that you can make sure that each object in the array is in order, like the other array? What if you do not know what keys and values ​​any objects inside arrays contain ... Significantly less than in what order they even exist?

So you need a "WildCard expression" for your [].filter , [].map , etc. How do you get a wild card expression?

 var jux = (function(){ 'use strict'; function wildExp(obj){ var keysCrude = Object.keys(obj), keysA = ('a["' + keysCrude.join('"], a["') + '"]').split(', '), keysB = ('b["' + keysCrude.join('"], b["') + '"]').split(', '), keys = [].concat(keysA, keysB) .sort(function(a, b){ return a.substring(1, a.length) > b.substring(1, b.length); }); var exp = keys.join('').split(']b').join('] > b').split(']a').join('] || a'); return exp; } return { sort: wildExp }; })(); var sortKeys = { k: 'v', key: 'val', n: 'p', name: 'param' }; var objArray = [ { k: 'z', key: 'g', n: 'a', name: 'b' }, { k: 'y', key: 'h', n: 'b', name: 't' }, { k: 'x', key: 'o', n: 'a', name: 'c' } ]; var exp = jux.sort(sortKeys); console.log('@juxSort Expression:', exp); console.log('@juxSort:', objArray.sort(function(a, b){ return eval(exp); })); 

You can also use this iteration function for each object to create the best collective expression for all keys in each of your objects, and then filter the array this way.

This is a small fragment from the Juxtapose API, which I have almost completed, which does this, equality of objects with exceptions, unity of objects and condensation of the array. If this is what you need or want for your project, please comment and I will make lib available sooner rather than later.

Hope this helps! Happy coding :)

0
Nov 09 '13 at 5:59
source share

 var names = [{ name: "Joe", age: 20, email: "joe@hotmail.com" }, { name: "Mike", age: 50, email: "mike@hotmail.com" }, { name: "Joe", age: 45, email: "mike@hotmail.com" } ]; const res = _.filter(names, (name) => { return name.name == "Joe" && name.age < 30; }); console.log(res); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script> 

0
Apr 24 '18 at 12:30
source share

For those who are looking for a filter from an array of JSON objects using any of the object / element keys

 function filterItems(items, searchVal) { return items.filter((item) => Object.values(item).includes(searchVal)); } let data = [ { "name": "apple", "type": "fruit", "id": 123234 }, { "name": "cat", "type": "animal", "id": 98989 }, { "name": "something", "type": "other", "id": 656565 }] console.log("Filtered by name: ", filterItems(data, "apple")); console.log("Filtered by type: ", filterItems(data, "animal")); console.log("Filtered by id: ", filterItems(data, 656565)); 

filter from an array of JSON objects: **

0
Sep 06 '19 at 10:12
source share



All Articles