Two arrays

I've got one array that I fetch from my local json file.
[
{"name":"namestring",
"img": "imgsrc"},
{"name":"namestring2",
"img": "imgsrc2"},
{"name":"namestring3",
"img": "imgsrc3"}
]
[
{"name":"namestring",
"img": "imgsrc"},
{"name":"namestring2",
"img": "imgsrc2"},
{"name":"namestring3",
"img": "imgsrc3"}
]
let cards = []

fetch("/data.json")
.then(res => res.json())
.then(data => {
cards = data.map(cards => {
return {Name: cards.name, Img: cards.img}
})
})
let cards = []

fetch("/data.json")
.then(res => res.json())
.then(data => {
cards = data.map(cards => {
return {Name: cards.name, Img: cards.img}
})
})
and say I've an empty array
let result = []
let result = []
I've a event listener on the input and
search.addEventListener("input" , (e) => {
const value = e.target.value.toLowerCase()
cards.forEach( card => {
card.Name.toLowerCase().includes(value)
})
});
search.addEventListener("input" , (e) => {
const value = e.target.value.toLowerCase()
cards.forEach( card => {
card.Name.toLowerCase().includes(value)
})
});
what I want to happen is if the input value is equal to the name from the array cards I want that whole object to be pushed to array result other wise return. Still learning javascript so would appreciate a visual example or a topic i should look for.
1 Reply
Jochem
Jochem2y ago
you probably want to use filter instead of forEach
Want results from more Discord servers?
Add your server