site
site

Reputation: 247

How to use correct filter in for loop using angular

I have this Array ["Let me introduce", "Our Values"]

I use this code:

 var dataname: string[] = item[4].namecategory;
         for (var i = 0; i < dataname.length; i++) {
         console.log(dataname[i]); // output dataname[i] Let me introduce dataname[i] Our Values
         }
    this.CrewChannelContents_category = this.CrewChannelContents.filter(x => x.Category.toLowerCase().includes(dataname[i].toLowerCase()));

show error

core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'toLowerCase' of undefined
TypeError: Cannot read property 'toLowerCase' of undefined

data from this.CrewChannelContents

[
                    {
                        Id: "15",
                        Category: "Our Values,WIDE",
                        Date: new Date('2019-12-02'),
                        
                    },
                    {
                        Id: "17",
                        Category: "Let me introduce",
                        Date: new Date('2019-12-05'),
                    
                    },.....
]

Updated code:

 let tmpVal;
                    for (var i = 0; i < dataname.length; i++) {
                        tmpVal = this.CrewChannelContents.filter(x => x.Category.toLowerCase().includes(dataname[i].toLowerCase()));
                        if (tmpVal) this.CrewChannelContents_category.push(tmpVal)
                        console.log(this.CrewChannelContents_category)
                    }

error

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined

in filter line.

Please can you share with me any idea, how to filter when I have data an array?

Thank you all

UPDATED CODE (2)

for (var i = 0; i < dataname.length; i++) {
                    console.log(this.CrewChannelContents.filter(x => x.Category.toLowerCase().includes(dataname[i].toLowerCase())));
                    this.CrewChannelContents_category = this.CrewChannelContents.filter(x => x.Category.toLowerCase().includes(dataname[i].toLowerCase()));
                    this.tmpvalue.push(this.CrewChannelContents_category);
                    console.log(this.tmpvalue)
                }

get an Array like image

Upvotes: 0

Views: 1734

Answers (2)

Kinglish
Kinglish

Reputation: 23664

Looks like you just need to include your filter check inside your loop:

export class SomeComponent implements OnInit {
   CrewChannelContents_category: string[];

   ngOnInit() {

        var data = [{
            Id: "15",
            Category: "Our Values,WIDE",
            Date: new Date('2019-12-02'),
        },
        {
            Id: "17",
            Category: "Let me introduce",
            Date: new Date('2019-12-05'),
        }]
    
    
    
        var dataname = ["Let me introduce", "Our Values"];
    
        // reset the CrewChannelContents_category variable
        this.CrewChannelContents_category = [];
        let tmpVal
        for (var i = 0; i < dataname.length; i++) {
            tmpVal = data.filter(x => x.Category.toLowerCase().includes(dataname[i].toLowerCase()));
            if (tmpVal) this.CrewChannelContents_category.push(tmpVal)
        }
    
    }

Upvotes: 0

Saptarshi Majumdar
Saptarshi Majumdar

Reputation: 41

It is because you use dataname[i] outside loop where i is greater than dataname length. So, for filtering you can use:

var data = [
  {
    Id: "15",
    Category: "Our Values,WIDE",
    Date: new Date("2019-12-02")
  },
  {
    Id: "17",
    Category: "Let me introduce",
    Date: new Date("2019-12-05")
  }
];
var dataname = ["Let me introduce", "Our Values"];
console.log(
  data.filter((x) =>
    dataname.some((y) => x.Category.toLowerCase().includes(y.toLowerCase()))
  )
);

Reference to how array.some() works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Upvotes: 0

Related Questions