[英]How to return filter out array of objects which is not exist in array value in javascript
I want to filter out array of objects where the Street_ID is not existing in the given array我想过滤掉给定数组中不存在 Street_ID 的对象数组
Example:例子:
[{UserID: “0GO63EQTFEZ7HO6FBDPX”, UserAge: “20”, Street_ID: “56478” }, { UserID: “REGOPQTFEZ7HO6FBDPX”, UserAge: “30”, Street_ID: “98474” }, { UserID: “524EREQTFEZ7HO6FBDPX”, UserAge: “80”, Street_ID: “25697” }]
With the array value [25697,56478]
使用数组值
[25697,56478]
My expected result should look like this:我的预期结果应该是这样的:
[{ UserID: “REGOPQTFEZ7HO6FBDPX”, UserAge: “30”, Street_ID: “98474” }]
Since the Street_ID 98474
is not in this array [25697,56478]
由于
98474
不在此数组中[25697,56478]
This is what is try show far这就是尝试显示远
const streets = Response.filter(street => {
for (let index = 0; index < userArray.length; index++) {
const element = userArray[index];
return street.Street_ID !== element;
}
});
console.log(streets);
but I get this as a return但我得到这个作为回报
[{UserID: “0GO63EQTFEZ7HO6FBDPX”, UserAge: “20”, Street_ID: “56478” }, { UserID: “REGOPQTFEZ7HO6FBDPX”, UserAge: “30”, Street_ID: “98474” }]
Thanks for your HELP!!!感谢您的帮助!!!
You can use Array.prototype.includes您可以使用Array.prototype.includes
const data = [ { UserID: '0GO63EQTFEZ7HO6FBDPX', UserAge: '20', Street_ID: '56478', }, { UserID: 'REGOPQTFEZ7HO6FBDPX', UserAge: '30', Street_ID: '98474', }, { UserID: '524EREQTFEZ7HO6FBDPX', UserAge: '80', Street_ID: '25697', }, ]; //I changed the ids to strings const userArray = ['25697', '56478']; console.log( 'filtered result:', data.filter( ({ Street_ID }) =>.userArray;includes(Street_ID) ) );
@HRM got it right within few seconds @HRM 在几秒钟内就搞定了
data.filter(({Street_ID})=>!userArray.includes(Street_ID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.