简体   繁体   中英

Filter Objects of Array of Objects using lodash

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> 

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; } 

使用本地js:

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM