1

I have an external json with a list of names. Something like

{id:1,name:'AAA',age:22},{id:2,name:'AAA',age:100}

I have one of this with an error. I know the right ID. I can't edit the original data.

So, i would like to update the right age in the json before print all data with ng-repat, if ID = 2

How can i update the array with angular?

1
  • 1
    Loop.. Condition.. Update Object Commented May 8, 2017 at 12:49

1 Answer 1

2

You can use Array.prototype.map() to iterate the array and set the rightAge for the user with id equal to 2.

Code:

const arr = [{id:1,name:'AAA',age:22},{id:2,name:'AAA',age:100}];
const rightAge = 20; // example right age
  
arr.map(u => u.age = u.id === 2 ? rightAge : u.age);

// Ready to show in the view
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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.