在 Typescript 中过滤整数数组 - Angular 2

贝克尔·奥德

我有一个数组[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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章