[英]Filtering integer array in Typescript - Angular 2
I have an array [2, 5, 8, 12, 20, "2, 8"]我有一个数组[2, 5, 8, 12, 20, "2, 8"]
How I can filter the integer (2), Like this [2, "2, 8"]我如何过滤 integer (2), 像这样[2, "2, 8"]
I converted my array to string and make a filter but the results are [2, 12, 20, "2, 8"]我将数组转换为字符串并进行过滤,但结果为[2, 12, 20, "2, 8"]
This is my code这是我的代码
this.news = this.news.filter(d => d.news_category.toString().indexOf(2) > -1);
You can split the each array item, and see if there is a "2" value using Array.some()您可以拆分每个数组项,并使用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.