I have a Objects of an array of objects as given below. I am trying to filter out each object within the array where is quantity is greater than 0 and drop it in a new variable. So via lodash I tried to use _.filter within _.(myArr).forEach. But this always returns an empty array. The console.log within _.filter shows quantity as it is but it doesn't return any value based on the condition. Am I using it in a proper way here or is there any other way I can use this to filter out?
var data = [
[{
"aid": "1",
"desc": "Desc 1",
"name": "Name 1",
"quantity": 1
}, {
"aid": "2",
"desc": "Desc 2",
"name": "Name 2",
"quantity": 1
}, {
"aid": "3",
"desc": "Desc 3",
"name": "Name 3",
"quantity": 0
}],
[{
"aid": "4",
"desc": "Desc 4",
"name": "Name 4",
"quantity": 0
}, {
"aid": "5",
"desc": "Desc 5",
"name": "Name 5",
"quantity": 1
}],
[{
"aid": "6",
"desc": "Desc 6",
"name": "Name 6",
"quantity": 0
}, {
"aid": "7",
"desc": "Desc 7",
"name": "Name 7",
"quantity": 0
}]
];
var filtered;
_(data).forEach((d) => {
filtered = _.filter(d, (o) => {
return o.quantity > 0;
});
});
console.log(filtered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.0/lodash.min.js"></script>