I want to update this array of objects
var a = [{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"none"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"none"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"none"},
{"uuid":"e468c4ff-98c8-4d53-986e-529e4a199540","answer":"none"},
{"uuid":"f71835b7-b57a-4971-9f0b-ff0474fd495e","answer":"none"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"none"}]
to
var a = [{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"found it"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"found it"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"found it"},
{"uuid":"e468c4ff-98c8-4d53-986e-529e4a199540","answer":"none"},
{"uuid":"f71835b7-b57a-4971-9f0b-ff0474fd495e","answer":"none"},
{"uuid":"93161c2b-6c56-4204-b6b5-c5c3cee0f38a","answer":"found it"}]
This only updates the first match in this array, but I need it to update all matches.
a.find(x => x.uuid == "93161c2b-6c56-4204-b6b5-c5c3cee0f38a").answer = "found it"
I know I can loop through the array to change each answer matching the uuid, but is there a better way to do this?
.forEach(). You could sort the array and then do a binary search, or (probably better) transform the array into a map from uuid value to a container object (or array of objects) with "answer" values or whatever else you want.