我有一个数组[2, 5, 8, 12, 20, "2, 8"]
我如何过滤整数 (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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句