Do people ever use DOM-based data storage/retrieval instead of arrays or objects to make the indexing simpler?
Consider the snippet below. To update the color to brown wherever name=Bob requires two loops in Javascript: one for the teams, and one for the players. Then a comparison to check for name=Bob before making the color change.
var obj = {"team1": {
"player1": {
"name": "bob",
"color": "red",
},
"player2": {
"name": "george",
"color": "blue",
},
},
"team2": {
"player1": {
"name": "bob",
"color": "orange",
},
"player2": {
"name": "george",
"color": "green",
},
}};
for (var i in obj)
for (var j in obj[i])
if (obj[i][j]["name"] == 'bob')
obj[i][j]["color"] = 'brown';
console.log(obj); // color changed to brown for the two bobs
But if the data was stored in the DOM, in table cell "data" attributes for example, it'd be easier to update the color where name=bob with one line of jQuery:
$("td[data-name='bob']").data("color", "brown");
My javascript object has about 1000 teams, with 4-5 players on each team. Looping through the whole thing for each color change seems like a lot of overhead. Whereas the jQuery seems to be able to hold the whole DOM in memory (true statement?) and cut straight to the right cells and make the change in color.
Or doe the jQuery actually have to perform two loops (or similar performance cost) behind the scenes in order to make the change?