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].
As VLAZ already commented forEach
doesn't return anything
const array = [{ name: "NAME", values: ["[John].[15]", "[Lois].[17]"] }]; const arrayFinal = array.filter((element) => element.values.some((el) => el.startsWith("[John].")) ); console.log(arrayFinal);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.