carreankush
carreankush

Reputation: 651

How to remove the particular array key values by comparing with the other array in angular

I have an array of object arraydata, on filtering I am getting its name field value. I need to remove the particular value by comparing with other array myarray. I mean need to compare comdata with myarray and remove the comdata values which are present in myarray. Here is the code below https://stackblitz.com/edit/angular-pyhpvi

app.component.ts

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular";
  arraydata = [
    { name: "name1", value: 1 },
    { name: "name2", value: 2 },
    { name: "name3", value: 3 },
    { name: "name4", value: 4 }
  ];
  ngOnInit() {
    console.log(this.arraydata);
    this.arraydata.filter(each => {
      const comdata = each.name;
      console.log(comdata);
    });
    const myarray = ["name1", "name2"];
    console.log(this.arraydata);
  }
}

Upvotes: 2

Views: 56

Answers (2)

Ehtesham Ahmad Nadim
Ehtesham Ahmad Nadim

Reputation: 431

Try this:

var result = arraydata.filter((item) => (!myarray.includes(item.name)));

Upvotes: 2

acincognito
acincognito

Reputation: 1743

Use this:

ngOnInit() {
  const myarray = ["name1", "name2"];
  this.arraydata = this.arraydata.filter(each => {
    return !myarray.includes(each.name);
  });
  console.log(this.arraydata);
}

Upvotes: 1

Related Questions