Ad
Error Cannot Read Property Name Fieldnamefilter Of Undefined When Filter Data?
I work on angular 7 app I face error cannot read property name 'fieldnamefilter' of undefined when filter data ?
filter name I get it dynamically based on variable fieldnamefilter
below code is give error on this.fieldnamefilter :
contentBody:any[];
fieldnamefilter:any;
console.log("data values : " + this.fieldnamefilter )
//result is data values : onlineURL on console log
// error display on line below filter data[0].this.fieldnamefilter
this.contentBody = data.filter(item =>item != data[0].this.fieldnamefilter);
but if i use it as data[0].onlineURL as below it working ;
//working when use field name direct data[0].onlineURL
this.contentBody = data.filter(item =>item != data[0].onlineURL);
data is have datatype any[]; so How to solve this issue ;
Updated post I try :
console.log(this.fieldnamefilter it give me onlineURL
console.log(data[0][this.fieldnamefilter] it give me N/A
Ad
Answer
const data = [{onlineurl: 'asdasd'}, {onlineurl: 'xxxx'}];
const fieldnamefilter = 'onlineurl';
const filteredData = data.filter(entry => entry[fieldnamefilter] === data[0][fieldnamefilter]);
This snippet works as intended, so as we can not debug the code without a MRE, I can not tell anything further. If you providea a MRE in Stackblitz or somewhere, we can help further. Also if your data look different than this please provide a sample.
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad