Filter Array Of Array For Values Inside Nested Array
I'm trying to get my head around this i have an array with nested arrays like so var array = [ [12, 43801309, '21.10.2018 00:00:00', 0.00089, 0.00055, 0.0004], [13, 43801308, '
Solution 1:
If the [index] is always the second element of the array, you can simply check whether the value at index 1 is the desired index.
var array = [
[12, 43801309, "21.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[13, 43801308, "22.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[34, 43801307, "23.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[234, 43801308, "24.10.2018 00:00:00", 0.00089, 0.00055, 0.0004]
];
var targetIndex = 43801308;
var filtered = array.filter(val => val[1] === targetIndex);
console.log(filtered);
Solution 2:
You can customize your filter condition
if(c.indexOf(43801308) > -1
if no check index
if(c[1] == 43801308)
if check index 1
let data = [
[12, 43801309, "21.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[13, 43801308, "22.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[34, 43801307, "23.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[234, 43801308, "24.10.2018 00:00:00", 0.00089, 0.00055, 0.0004]
];
let result = data.filter(c=> { if(c.indexOf(43801308) > -1) return c;});
console.log(result);
let data = [
[12, 43801309, "21.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[13, 43801308, "22.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[34, 43801307, "23.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[234, 43801308, "24.10.2018 00:00:00", 0.00089, 0.00055, 0.0004]
];
let result = data.filter(c=> { if(c.indexOf(43801308) > -1) return c;});
//if only need index 1
result = data.filter(c=> { if(c[1] == 43801308) return c;});
console.log(result);
Solution 3:
array.filter(function(arr) {
return arr[1] === value;
}
This should solve your problem. You don't seem to want to filter the nested array, but rather choose the ones where a value is included.
Solution 4:
You don't want to filter twice. What you really want to achieve is filter rows that match a certain condition, in your case, the condition is row[1] === lookupVal
.
const array = [
[12, 43801309, "21.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[13, 43801308, "22.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[34, 43801307, "23.10.2018 00:00:00", 0.00089, 0.00055, 0.0004],
[234, 43801308, "24.10.2018 00:00:00", 0.00089, 0.00055, 0.0004]
];
const lookupVal = 43801308;
const filterRows = array.filter(row => row[1] === lookupVal);
console.log(filterRows);
Post a Comment for "Filter Array Of Array For Values Inside Nested Array"