.Filter Not a Function Error

.filter is not a function

filter is a method on arrays. Since the code you posted contains an object, you're seeing this error. You may want to apply filter after getting all the values from the object using Object.values, like this:

var users = {  "1": {    "user_id": 1,    "test": "",    "user_name": "potato0",    "isok": "true"  },
"2": { "user_id": 2, "test": "", "user_name": "potato1", "isok": " true" },
"3": { "user_id": 3, "test": "", "user_name": "potato2", "isok": " true" },
"4": { "user_id": 4, "test": "", "user_name": "potato3", "isok": "locationd" }};
console.log(Object.values(users).filter(user => user.user_id === 1));

Uncaught TypeError: [Array].filter is not a function

Your code seems to work properly as you can see here

You might want to refactor it to be sure to have access to the data in search results when you need it:

var queryUrl = "http://127.0.0.1:5000/api/1/GenderOverTime";
var searchResults = [];

d3.json(queryUrl).then(function (chart_data) {
searchResults = chart_data;
init(searchResults);
});


function init(results) {
console.log("searchResults: ", results);
let selector = d3.select("#selDataset");
let options = [];
filtered_data_for_chart = results.filter((result) => {
if (!options.includes(result.Sport)) {
options.push(result.Sport);
selector
.append("option")
.text(result.Sport)
.property("value", result.Sport);
}

return result.Sport === "Gymnastics";
});
}

Uncaught TypeError: arr.filter is not a function

Seems like the arr you are sending to the filter function is not an array, which is why the error is saying that arr.filter is not a function.

Just tried this and it works, so your function seems ok:

function filter(arr, criteria) {
return arr.filter(function (obj) {
return Object.keys(criteria).every(function (c) {
return obj[c] == criteria[c];
});
});
}

const arr = [
{
name: "David",
age: 54
},
{
name: "Andrew",
age: 32
},
{
name: "Mike",
age: 54
}
];
const myFilter = {"age": 54};
const result = filter(arr, myFilter);

console.log(result);

Item.filter is not a function

const items = await Item.find(({userId:req.user.userId}).lean();

it should return exact items from db that you want you can use more query if you need.

.filter not a function error?

You have to use an array like so:

function sumTwoSmallestNumbers(numbers) {  const filter = numbers.filter(x => x > -1).sort((a, b) => a - b);  return filter[0] + filter[1];}
console.log(sumTwoSmallestNumbers([544, 32654, 34297, 9237, 343, 98])); // 441

Uncaught TypeError: work.filter is not a function

error occurs when you call work.filter it seems to me that the work is not an array due to which there is no function named filter to call, that's why error is thrown. Make sure your imported array is 2 dimensional meaning it's elements are also arrays



Related Topics



Leave a reply



Submit