0

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?

2
  • 2
    You need to loop through the array to change each answer matching the uuid. Commented Dec 26, 2021 at 12:36
  • If the id values are in essentially random order, all you can do is sequentially search through the array with a loop or .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. Commented Dec 26, 2021 at 12:37

2 Answers 2

3

A short and simple way to loop through them all and change them would be with forEach:

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"}];

a.forEach((elem, idx) => {
  if (elem.uuid == "93161c2b-6c56-4204-b6b5-c5c3cee0f38a") { 
    elem.answer = "found it";
  }
});

console.log(a);

Alternatively, you can use map, another shorthand way to do effectively what you described. It returns a new array:

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"}];

a = a.map(elem => elem.uuid == "93161c2b-6c56-4204-b6b5-c5c3cee0f38a" ? ({ ...elem, answer: "found it" }) : elem);

console.log(a);

Note that this still will loop through the array, but it's a neater way to do it (creating a new array based off of your original one). I used object spreading { ...elem } syntax above so that it'll preserve other properties in your objects too if there are more. Another, more verbose way to do this would be:

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"}];

a = a.map(elem => {
  if (elem.uuid == "93161c2b-6c56-4204-b6b5-c5c3cee0f38a") { 
    elem.answer = "found it";
  }
  return elem;
});

console.log(a);

Sign up to request clarification or add additional context in comments.

2 Comments

Or .forEach(), which would not incur the (very minor) overhead of making a new array
Yes @Pointy, just about to mention that one - realised it'd probably be more effective here since only some items require mutation. Thanks!
1

There are 2 ways to achieve the result:

1) Using forEach: (Preferred way)

var data = [{"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"}]

const updateRecord = (data, id) => {
  data.forEach((record) => {
    if(record.uuid === id){
      record.answer = 'found it'
    }
    return record
  })
  
  return data
}

console.log(updateRecord(data, "93161c2b-6c56-4204-b6b5-c5c3cee0f38a"))

2) Using map:

var data = [{"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"}]


const updatedData = data.map(record => record.uuid == "93161c2b-6c56-4204-b6b5-c5c3cee0f38a" ? ({ ...record, answer: "found it" }) : record);

console.log(updatedData);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.