I am currently working on generating a filtered, sorted, and sliced list of data items in my Angular application. After successfully sorting and slicing the data, I encountered an issue when using dataHandler.filter
. The error message
'Cannot read property 'slice' of undefined'
is being displayed.
Within my controller, I am attempting to create a new list by executing these functions:
Controller
getData().then(function(data) {
function updateChart() {
// obtain filter value
var filterValue = inputService.primaryInputs[0]["value"];
// apply filtering based on description match
var filtered = dataHandler.filter(data, "Description", filterValue);
// sort by descending percent value
var sorted = dataHandler.sort.descending(filtered, "Percent");
// display only top 5 results
var sliced = dataHandler.slice(sorted, 5);
$scope.barData = sliced;
}
updateChart();
});
I have verified that dataHandler.sort.descending
and dataHandler.slice
are functioning correctly as intended. However, encountering issues specifically with filtered
when used as an argument in dataHandler.sort.descending
, resulting in the aforementioned error.
Factory
app.factory('dataHandler', function ($rootScope) {
return {
filter: function(data, dataProp, input) {
data.filter(function(value, index, array) {
console.log(value[dataProp] == input);
return value[dataProp] == input;
});
},
sort: {
ascending: function (data, sortCriteria) {
if (data) {
data.sort(function (a, b) {
return a[sortCriteria] - b[sortCriteria];
});
};
return data;
},
descending: function (data, sortCriteria) {
if (data) {
data.sort(function (a, b) {
return b[sortCriteria] - a[sortCriteria];
});
};
return data;
}
},
slice: function (data, howMany) {
if (howMany) {
return data.slice(0, howMany);
} else {
return data;
}
}
};
In essence, I am looking to utilize dataHandler.filter
to refine the list based on matching criteria from the Description
values to the filterValue
.