I have a big array of objects that i got from my rest api and i want to search for a value from one of the elements.
I managed to come up with a method that searches the words but it works for a few words and not all. when i tried to find out the problem i realised that the for loop doesnot go through the whole array.
below is my code
function search(nameKey, myArray) {
var lowercased_array = [];
//lowercase the search value
var lowercased_nameKey = nameKey.toLowerCase()
//lowercase the searched array
for (var i = 0; i < myArray.length; i++) {
myArray[i].name = myArray[i].name.toLowerCase()
lowercased_array.push(myArray[i])
}
var new_array = [];
for (var i = 0; i < lowercased_array.length; i++) {
if ((lowercased_array[i].name).includes(lowercased_nameKey)) {
new_array.push(lowercased_array[i])
}
}
return new_array
}
i first turn the search word to lowercase , my first for loop is to turn the values for the name element in the array objects to lower case so that the search can take place.
The second for loop is to check if the search value exists in the name element of the objects in the array and if true push the whole object in a new array.
i am asking for help on how to ensure that the process is complete before returning the new array with the search results.
Thanks in advance
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…