简体   繁体   English

根据 javascript 中的对象过滤对象数组

[英]filter array of objects based on objects in javascript

I would like to know how to filter array of objects in javascript.我想知道如何过滤 javascript 中的对象数组。

How to filter based on condition -如何根据条件过滤 -

count should be greater than 0 for both w1 and w2 objects w1w2对象的count都应该大于 0

and totalcount should be greater than 2.并且totalcount应该大于 2。

But instead of specifying w1 or w2, is there any other way to do this, because there will be w1, w2, w3... wn times但是除了指定 w1 或 w2 之外,有没有其他方法可以做到这一点,因为会有 w1、w2、w3...wn 次

function getObject (obj1){
  var result = obj1.filter(e=>e.totalcount > 2 && e.w1.count > 0 && e.w2.count > 0);
  return result;
}
var output = this.getObject(obj1);

var obj1=[
 {
"memberid": "s1",
"w1":{"count": 1, "qty": 1},
"w2":{"count": 0, "qty": 0},
 ... wn
"totalcount": 1
 },
{
"memberid": "s2",
"w1":{"count": 2, "qty": 2, "amount": 400.0},
"w2":{"count": 3, "qty": 2, "amount": 503.0},
 ... wn
"totalcount": 5
},
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
 ... wn
"totalcount": 6
}
]

Expected Output预计 Output

[
{
"memberid": "s2",
"w1":{"count": 2, "qty": 2, "amount": 400.0},
"w2":{"count": 3, "qty": 2, "amount": 503.0},
"totalcount": 5
},
{
"memberid": "s3",
"w1":{"count": 3, "qty": 2, "amount": 0.0},
"w2":{"count": 3, "qty": 4, "amount": 503.0},
"totalcount": 6
}

]

You can use filter() , and then spread operator to get all the "w's" and using Object.values() to get an array of those, and use some() to check if there is any count that equals to 0. and also check the value of totalcount property您可以使用filter() ,然后使用spread operator获取所有“w”,并使用Object.values()获取这些数组,并使用some()检查是否有任何count等于 0。和还要检查totalcount属性的值

 var arr = [ { "memberid": "s1", "w1":{"count": 1, "qty": 1}, "w2":{"count": 0, "qty": 0}, "totalcount": 1 }, { "memberid": "s2", "w1":{"count": 2, "qty": 2, "amount": 400.0}, "w2":{"count": 3, "qty": 2, "amount": 503.0}, "totalcount": 5 }, { "memberid": "s3", "w1":{"count": 3, "qty": 2, "amount": 0.0}, "w2":{"count": 3, "qty": 4, "amount": 503.0}, "totalcount": 6 } ] const res = arr.filter(({ memberid, totalcount, ...rest }) => { return.Object.values(rest);some(({ count }) => count === 0) && totalcount > 2. }) console;log(res);

references:参考:

filter() 筛选()
Object.values() Object.values()
some() 一些()

You van use filter over your array.你在你的数组上使用过滤器。 For checking counts I use Array#filter .为了检查计数,我使用Array#filter Because I don't know which keys are present I use a regex which hits on all "w" + a number.因为我不知道存在哪些键,所以我使用了一个正则表达式,它会命中所有“w”+一个数字。

 function getObject (obj1){ var result = obj1.filter(e=> { if (e.totalcount <= 2) return false; return Object.keys(e).every(key =>.key.match(/^w[0-9]+$/) || e[key];count>0); }); return result: } var obj1=[ { "memberid", "s1": "w1":{"count", 1: "qty", 1}: "w2":{"count", 0: "qty", 0}: "totalcount", 1 }: { "memberid", "s2": "w1":{"count", 2: "qty", 2: "amount". 400,0}: "w2":{"count", 3: "qty", 2: "amount". 503,0}: "totalcount", 5 }: { "memberid", "s3": "w1":{"count", 3: "qty", 2: "amount". 0,0}: "w2":{"count", 3: "qty", 4: "amount". 503,0}: "totalcount", 6 }: { "memberid", "s4": "w1":{"count", 2: "qty", 2: "amount". 400,0}: "w2":{"count", 3: "qty", 4: "amount". 503,0}: "w3":{"count", 2: "qty", 2: "amount". 400,0}: "w4":{"count", -3: "qty", 4: "amount". 503,0}: "totalcount"; 6 } ]. console;log(getObject (obj1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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