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));
Answer from 31piy on Stack OverflowBecause filter is an array function (Array.prototype.filter), while you are calling it on a string. str.split returns an array and doesn't change anything to your str. call it like console.log(str.split('').filter(fil)) and it should be fine.
Because you're invoking the execution of "filter" on str, which is an object without a function called "filter" of its own nor by prototype. Since filter is not present the property's value is undefined, which cannot be invoked because its type is not function.
Working with react. working with the state of
data
when i checked
console.log(typeof(data))
it says it is a array.
however when i put
{typeof(data)}it says it is a object
the render very occasionally work, but most of the time it spills × TypeError: data.filter is not a function. why?
typeof(data) should never say array. Arrays will always report as "object" to typeof. To see if something is an array, use Array.isArray(). If filter is not a function, it's also very likely that what you have is not an array.
Not related to your issue, but typeof is not a function, and should be written typeof data.
So I'm going through Andrew Mead's Modern Javascript Bootcamp, and during a fairly simple challenge ended up with an error that has stopped me in my tracks. The challenge was to merely refactor some existing code into a new file, which went fine until the part where I had to merely cut and paste the block of code below. All of a sudden after that the application no longer works and is giving me the error
"Uncaught TypeError: todos.filter is not a function
at renderTodos (todo-functions.js:19)
at todo-app.js:8
renderTodos @ todo-functions.js:19
(anonymous) @ todo-app.js:8"
I've fiddled around with it and even gone as far as to copy in the instructor's code that is provided as a resource, and still, I get the same error. Even when I moved it back around to how it was prior, still this same error. I posted in the course but it looks like it could be a while before anyone sees and answers, so I was wondering if anyone here had any ideas as to what might be going on? I can post more code from this but figured for now I would just include the snippet that's seemingly affected.
Any help would be greatly appreciated, I would just move on but unfortunately, without it working I can't really continue to the next lessons. Thanks!
// Render application todos based on filters
const renderTodos = function (todos, filters) {
const filteredTodos = todos.filter(function (todo) {
const searchTextMatch = todo.text
.toLowerCase()
.includes(filters.searchText.toLowerCase());
const hideCompletedMatch = !filters.hideCompleted || !todo.completed;
return searchTextMatch && hideCompletedMatch;
});
const incompleteTodos = filteredTodos.filter(function (todo) {
return !todo.completed;
});
document.querySelector("#todos").innerHTML = "";
document
.querySelector("#todos")
.appendChild(generateSummaryDOM(incompleteTodos));
filteredTodos.forEach(function (todo) {
document.querySelector("#todos").appendChild(generateTodoDOM(todo));
});
};