Example: If I use the code below it works for the whole word "gorilla".
How do I get it to work with "gor" or "illa" or "orill"...?
...(equivalent to a str like "%gor%" in mysql for example)
const jungle = [
{ name: "frog", threat: 0 },
{ name: "monkey", threat: 5 },
{ name: "gorilla", threat: 8 },
{ name: "lion", threat: 10 }
];
const names = jungle.map(el => el.name);
// returns true
document.write(names.includes("gorilla"));
Answer
You can use find
(or filter
)
const jungle = [
{ name: "frog", threat: 0 },
{ name: "monkey", threat: 5 },
{ name: "gorilla", threat: 8 },
{ name: "lion", threat: 10 }
];
const names = (partial) => jungle.find(el => el.name.includes(partial)).name;
//You can also use "filter" method ==>
//const names = (partial) => jungle.filter(el => el.name.includes(partial)).map(el => el.name)
console.log(names("gor"))
console.log(names("illa"))
console.log(names("orill"))
Comments
Post a Comment