[英]Javascript filter, foreach, startsWith on array
I need to be clarified on a little point, I'd like to filter an array only with some starting specific element.我需要澄清一点,我想只过滤一个带有一些起始特定元素的数组。
I've tried something like this:我试过这样的事情:
const array = [{ "name": "NAME", "values": ["[John].[15]", "[Lois].[17]"] }]; const arrayFinal = array.filter(element => element.values.forEach(el => el.startsWith("[John]."))); console.log(arrayFinal);
But the printed array is empty.但是打印的数组是空的。 Do I miss something?
我错过了什么吗?
Thank you by advance提前谢谢你
use some in place of forEach
to return true
if any of the element starts with [John].
如果任何元素以
[John].
开头,则使用some代替forEach
返回true
。 As VLAZ already commented forEach
doesn't return anything正如 VLAZ 已经评论的那样,
forEach
没有返回任何东西
const array = [{ name: "NAME", values: ["[John].[15]", "[Lois].[17]"] }]; const arrayFinal = array.filter((element) => element.values.some((el) => el.startsWith("[John].")) ); console.log(arrayFinal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.