Skip to content

Instantly share code, notes, and snippets.

@Nicanor008
Created October 30, 2020 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nicanor008/09f6c1078625009bb8bf18dee96c5f63 to your computer and use it in GitHub Desktop.
Save Nicanor008/09f6c1078625009bb8bf18dee96c5f63 to your computer and use it in GitHub Desktop.
Array objects can be tricky to update. To avoid repetition, here is a snippet to find by key/name and update value.
var data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
];
function updateData(obj){
var objFound_bool = false;
for (var i = 0; i < data.length; i++) {
if(obj.name === data[i].name){
objFound_bool = true;
data[i].value =obj.value ;
}
}
if(!objFound_bool){
data.push(obj)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment