0

I want to do bulk delete from an array using typescript.

I did it by using for loop.

this.versions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
this.selectedVersions = [1, 2, 3, 4, 5, 6];
for (let i = 0; i < this.selectedVersions.length; i++) {
  this.versions = this.removeObjectFromArray(this.versions, this.selectedVersions[i]);
}


//removing a object from array
removeObjectFromArray(listOfdata: any, data: any) {
  let index = listOfdata.findIndex(function (o) {
    return o === data;
  })
  listOfdata.splice(index, 1);
  return listOfdata;
}

But I don't like to use for loop.so let me know how to do bulk delete in array using typescript.

7 Answers 7

5

You can just use array filter:

var versions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var selectedVersions = [1, 2, 3, 4, 5, 6];

versions = versions.filter(el => !selectedVersions.includes(el));
console.log(versions);

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

Comments

2

Use a filter

// will only keep elements that are not included in selectedVersions
this.versions = this.versions.filter(i => !this.selectedVersions.includes(i));

Comments

2

Make use of array.filter and filter the contents of the arrays based on the condition

let versions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let selectedVersions = [1, 2, 3, 4, 5, 6];
versions = versions.filter(arrayVal => selectedVersions.indexOf(arrayVal) == -1 );
// checks for the values of selected Versions agains each value of versions and returns accordindly
console.log(versions)

3 Comments

Just curious, is there a reason you don't use includes instead of indexOf?
I didn't see to it really @Matt this was the first thing that stuck my small mind :p
It's fine either way. Both works. Just was curious if it was a performance thing or not.
1

I think you can use indexOf, like this:

this.versions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
this.selectedVersions = [1, 2, 3, 4, 5, 6];
  for (let i = 0; i < this.selectedVersions.length; i++)  
  {  
if(this.versions.indexOf(this.selectedVersions[i])!==-1){
this.versions.splice(index, 1);
}
  }

1 Comment

Actually I don't want use for loops.
1

I would personally use a filter for this to preserve the original arrays:

let nonSelectedVersions = this.versions.filter((e) => this.selectedVersions.indexOf(e) !== -1);

Comments

0

Use array.filter() instead of for loop

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Comments

0

Typescript is the same as javascript, you can us all your knowledges from JS.

How do I remove a particular element from an array in JavaScript?

You can use for-of loop as well, if you TS compiler support

var versions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var selectedVersions = [1, 2, 3, 4, 5, 6];
    for ( val of  selectedVersions)  {        
       if(selectedVersions.indexOf(val)!=-1){
           versions.splice(versions.indexOf(val), 1);
       }
    }

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.