4

I want to do bulk update to an array using typescript.

I did it by using for loop.

this.versions = [{ id: 1, VersionName: 'test1' }, { id: 2, VersionName: 'test2' }, { id: 3, VersionName: 'test3' }, { id: 4, VersionName: 'test4' }];
this.selectedVersions = [{ id: 2, VersionName: 'test2' }, { id: 3, VersionName: 'test3' }];
for (let i = 0; i < this.selectedVersions.length; i++) {
  this.versions = this._configuration.updateEveryObjectsByKey(this.versions, this.selectedVersions[i], "Id");
}

updateEveryObjectsByKey(listOfdata: any, data: any, key: string) {
  listOfdata = listOfdata.map(function (item) {
    return item[key] == data[key] ? data : item;
  })
  return listOfdata;
}

But I don't like to use for loop. So let me know how to do bulk update to array using typescript?

6
  • What about the question you asked 25 minutes ago ? It has 7 answers. Commented Oct 24, 2017 at 14:48
  • That's related with multiple delete by using findIndex or filter. But this multiple update by using map or else. So I have asked separate question . Commented Oct 24, 2017 at 14:49
  • 1
    Ok, fair enough Commented Oct 24, 2017 at 14:50
  • Your current code doesn't actually modify the original objects. Instead it will completely replace the original object with the new object from selectedVersions. Is this a functionality you want to keep? Commented Oct 24, 2017 at 14:58
  • 1
    There's a big difference between update and replace, for example you have { id: 0, a: 2, b: 3 }. Update with { id: 0, a: 3 } will result in { id: 0, a: 3, b: 3 } while replace will result in { id: 0, a: 3 }. Commented Oct 24, 2017 at 15:07

2 Answers 2

9

You can use ES6's Object.assign, array.find and array.map :

var versions = [{ id: 1, VersionName: 'test1' }, { id: 2, VersionName: 'test2' }, { id: 3, VersionName: 'test3' }, { id: 4, VersionName: 'test4' }];
var selectedVersions = [{ id: 2, VersionName: 'test2 update' }, { id: 3, VersionName: 'test3' }];
var key = "id";
versions = versions.map(el => {
  var found = selectedVersions.find(s => s[key] === el[key]);
  if (found) {
      el = Object.assign(el, found);
  }
  return el;
});
console.log(versions);

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

3 Comments

In this solution, the key used to compare is hardcoded to id whereas the OP sets it dynamically in the current code.
@BeetleJuice What is exactly am typing. I want to it by using key instead of id. because I would use this method as a common method. :)
So I think I need to change s.id === el.id to s[key] === el[key]??
1

Simple solution to a potentially complex problem.

           $scope.model.ticketsArr.forEach(function (Ticket) {
                if (Ticket.AppointmentType == 'CRASH_TECH_SUPPORT') {
                    Ticket.AppointmentType = '360_SUPPORT'

                }
            });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.