0

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>

3 Answers 3

2

You are overwriting filtered in each iteration, so its value will represent only what happened in the last iteration of the forEach loop.

You should instead accumulate the individual results into an array, for instance with push and the spread syntax:

var filtered = [];

... and in the loop:

    filtered.push(..._.filter(d, (o) => o.quantity > 0);

Note that you can do this in vanilla JavaScript, using array methods like reduce and filter:

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.reduce( 
    (filtered, d) => filtered.concat(d.filter( o => o.quantity > 0 )),
    []
);

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome.. Thank you so much for the detailed response.. :)
Which of the above approaches you suggest as the best to adapt? :)
Since JavaScript ES5 and certainly ES6 there is less reason to use a library as lodash, but if you need it for other purposes which cannot be written that well in ES6, then of course you can use it throughout.
Well yeah, I am using lodash in most of the areas of the project, so I would be going with the same.. Thank you so much once again.. :)
2

using native js:

let filtered = data.reduce((a, e) => a.concat(e)).filter(x => x.quantity > 0);

3 Comments

There is more data than only in data[0].
data.reduce((a, e) => a.concat(e)), [...data.reduce((a, e) => a.concat(e))], [...data.reduce((a, e) => a.concat(e))].reduce((a, e) => a.concat(e), []) -> There three will produce the same output, isn't it? Why don't we directly write data.reduce((a, e) => a.concat(e)) .filter(x => x.quantity > 0);. Am I missing something?
@RaR you are absolutely right. I've got lost mixing approaches. I'm very glad you'd notice it, cheers!
1

Or alternatively you can use reduce

var output = data.reduce( 
    ( acc, c ) => acc.concat( c.filter( 
           s => s.quantity > 0 ) )  //filter out quantities > 0
    ,[]); //initialize accumulator to [] 

Demo

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 output = data.reduce( ( acc, c ) => acc.concat( c.filter( s => s.quantity > 0 ) )  ,[]); 
console.log(output);

4 Comments

That errors out.. c.filter is not a function.. :(
Ohh, let me share a demo with you. C is an array so it must have a filter method.
@GuruprasadRao Please check the updates, I have attached a demo now.
Thank you so much bro. I would like to go with the accepted answer. But nonetheless, this will be useful somewhere in future.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.