Is there a better way to do this in JavaScript? Array search using while -
i'm thinking of ways improve next piece of code, minimizing lines of code, keeping clear reader code does. somehow utilize indexof()
in illustration below?
var events = myarray.data.items; info = null; found = false; counter = 0; if (selectedvalue != "") { while (!found && counter < events.length) { //notice there's layer of objects each object in array if (events[counter].data.event_id == selectedvalue) { info = events.items[counter].data; //do stuff found = true; break; } counter++; } }
you can trick using some() method:
var info = null; var found = events.some(function (event) { homecoming event.data.event_id == selectedvalue ? ((data = event.data), true) : false; });
javascript
No comments:
Post a Comment