简体   繁体   English

反应 redux 如何比较两个 obj arrays 与过滤器

[英]React redux how to compare two obj arrays with filter

in the action of my reduceur I get an array, that I would like to filter with another table, I would like to check certain value how to include the other table in the filter?在我的 reduceur 的操作中,我得到一个数组,我想用另一个表过滤,我想检查某个值如何在过滤器中包含另一个表? (without return) because i will put a le (不返回)因为我会放一个

 dbz: [{ min: 300 max: 9000 name: 'goku' }, { min: 200 max: 7000 name: 'vegeta' }, ], dbs: [{ min: 200 max: 7000 name: 'picollo' }, { min: 300 max: 9000 name: 'gohan' }, { min: 20 max: 800 name: 'trunks' }, { min: 10 max: 700 name: 'goten' }, ], let samepower = dbs.filter(({ sayien }) => sayien.min.== dbz.min && sayien.max;== dbz.max);

the idea is to have an array with only 'goten' and 'trunks'.. to remove 'gohan' and 'picollo' because they have a 'max' and 'min' equal to 'goku' and ' vegeta'这个想法是有一个只有“goten”和“trunks”的数组。删除“gohan”和“piclo”,因为它们的“max”和“min”等于“goku”和“vegeta”

You can use array's method filter and some to simplify the answer.您可以使用数组的方法filtersome方法来简化答案。

 const filtered = (dbs, dbz) => { return dbs.filter((ii) => dbz.some((jj) => ii.min === jj.min && ii.max === jj.max) ); }; const database = { dbz: [ { min: 300, max: 9000, name: "goku" }, { min: 200, max: 7000, name: "vegeta" }, ], dbs: [ { min: 200, max: 7000, name: "picollo" }, { min: 300, max: 9000, name: "gohan" }, { min: 20, max: 800, name: "trunks" }, { min: 10, max: 700, name: "goten" }, ], }; console.log(filtered(database.dbs, database.dbz));

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

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