Created
October 30, 2020 05:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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