[英]Filtering integer array in Typescript - Angular 2
我有一个数组[2, 5, 8, 12, 20, "2, 8"]
我如何过滤 integer (2), 像这样[2, "2, 8"]
我将数组转换为字符串并进行过滤,但结果为[2, 12, 20, "2, 8"]
这是我的代码
this.news = this.news.filter(d => d.news_category.toString().indexOf(2) > -1);
您可以拆分每个数组项,并使用Array.some()查看是否有“2”值
const input = [2, 5, 8, 12, 20, "2, 8"] const output = input.filter(d => d.toString().split(",").some(val => val.trim() === "8")) console.log(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.