How To Filter Multidimensional Javascript Array
I have this data: var object = [{ 'nid': '31', '0': { 'tid': '20', 'name': 'Bench Press', 'objectDate': '2012-02-08', 'goal': 'rep',
Solution 1:
There is no function for this in Javascript. You have to write your own function like this.
var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}];
functionfilterByProperty(array, prop, value){
var filtered = [];
for(var i = 0; i < array.length; i++){
var obj = array[i];
for(var key in obj){
if(typeof(obj[key] == "object")){
var item = obj[key];
if(item[prop] == value){
filtered.push(item);
}
}
}
}
return filtered;
}
var byName = filterByProperty(arr, "name", "Fran");
var byGoal = filterByProperty(arr, "goal", "time");
Solution 2:
The question is about multidimensional arrays. If you like me missed that here are solutions for normal arrays...
2020
filteredArray = array.filter(item => item.name.indexOf('Fran') > -1);
or
filteredArray = array.filter(function(item)
{
return item.name.indexOf('Fran') > -1);
}
2012 version
var result = [];
for (var i = 0; i < array.length; i++)
{
if (array[i].name === 'Fran')
{
result.push(array[i]);
}
}
Solution 3:
I would create a function for filtering :
functionfilter(array, key, value){
var i, j, hash = [], item;
for(i = 0, j = array.length; i<j; i++){
item = array[i];
if(typeof item[key] !== "undefined" && item[key] === value){
hash.push(item);
}
}
return hash;
}
A more robust solution might be adding a filter method to the prototype:
`This prototype is provided by the Mozilla foundation and
is distributed under the MIT license.
http://www.ibiblio.org/pub/Linux/LICENSES/mit.license`if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun/*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis)
{
varval = this[i]; // in case fun mutates thisif (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
Then simply call:
functionfilterName (item, index, array) {
return (item.name === "Fran");
}
var result = object.filter(filterName);
Solution 4:
If your object contains 3 fields, say email, firstName, lastName then you use this.
ArrayObject
.filter(em => em.email.indexOf(searchEmail) > -1)
.filter(fn => fn.firstName.indexOf(searchFirstName) > -1)
.filter(ln => ln.lastName.indexOf(searchLastName) > -1)
Solution 5:
The big trick is to make a flat array with just the item you want in it.
say you have a 2d array like so:
e = [[1,2],[1,2],[2,3],[4,5],[6,7]]
you make a new array:
var f = []
fill it with just 1 item:
for(var x=0;x<e.length;x++) f[x] = e[x][1]
giving us the likes of:
f = [2,2,3,5,7]
and then....
var g = e.filter( function(elem, pos){ return (f.indexOf(elem[1]) == pos) })
cowabunga!
Post a Comment for "How To Filter Multidimensional Javascript Array"