ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.4k

when filter data by using fieldnamefilter variable it give m

Jun 8 2020 5:10 PM
work on angular 7 app I face Issue when filter data using data[0][this.fieldnamefilter] it give me N/A
 
I filter data by using variable fieldnamefilter
 
below code is give n/a on this.fieldnamefilter function filter :
contentBody:any[]; 
fieldnamefilter:any; data is have datatype any[];
console.log("data values : " + this.fieldnamefilter ) //result is data values : onlineURL on console log
// when use data[0][this.fieldnamefilter] it give me N/A
this.contentBody = data.filter(item =>item != data[0][this.fieldnamefilter]);
but If I use it as data[0].onlineURL it working ; //working when use field name direct data[0].onlineURL
this.contentBody = data.filter(item =>item != data[0].onlineURL);
 
so How to solve this issue ;
 
Updated post I try :
 
console.log(this.fieldnamefilter it give me onlineURL
 
when use field name direct it work but on my case i stored filter name on variable
 
so how to use variable name on data[0].variable have filter
 
if you store a field in that variable, you can use it like this:
 
data[0][this.fieldnamefilter] const data = [{onlineurl: 'asdasd'}, {onlineurl: 'xxxx'}];
const fieldnamefilter = 'onlineurl';
const filteredData = data.filter(entry => entry[fieldnamefilter] === data[0][fieldnamefilter

Answers (1)