Skip to content

Instantly share code, notes, and snippets.

@GustavoAdolfo
Last active February 23, 2020 02:50
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 GustavoAdolfo/ad479bb454374b5090bd3afcca249791 to your computer and use it in GitHub Desktop.
Save GustavoAdolfo/ad479bb454374b5090bd3afcca249791 to your computer and use it in GitHub Desktop.
Alterando valor de objeto com foreach
var arrayzao = [
[ //arrayzinho
{ id: 1, nome: 'abc', valor: 10.5 },
{ id: 2, nome: 'def', valor: 9.99 },
{ id: 3, nome: 'ghi', valor: 5.75 }
],
[ //arrayzinho
{ id: 1, nome: 'abc', valor: 11.5 },
{ id: 2, nome: 'def', valor: 10.0 },
{ id: 3, nome: 'ghi', valor: 7.0 },
{ id: 4, nome: 'jkl', valor: 8.0 }
],
];
function incrementar (arr, valor, id) {
if (!Array.isArray(arr)) {
return;
}
arr.forEach(elem => {
if (elem.id === id) {
elem.valor += valor;
}
});
}
arrayzao.forEach(item => incrementar(item, 5, 3));
console.log('arrayzao', arrayzao);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment