I'm thinking of ways to improve the following piece of code, mostly by minimizing the lines of code, but also keeping it clear to the reader what the code does. Could I somehow use indexOf()
in the example below?
var events = myArray.data.items;
data = null;
found = false;
counter = 0;
if (selectedValue != "") {
while (!found && counter < events.length) {
//notice there's an extra layer of objects for each object in the array
if (events[counter].data.Event_ID == selectedValue) {
data = events.items[counter].data;
//do stuff
found = true;
break;
}
counter++;
}
}
You can do a trick using the some() method:
var data = null;
var found = events.some(function (event) {
return event.data.Event_ID == selectedValue ? ((data = event.data), true) : false;
});
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.