.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
How to Change Height in Mat-Form-Field
Jest Encountered an Unexpected Token: Syntaxerror: Unexpected Token {
Looping Through a Json Object Containing More Objects and Arrays
Jquery Wait Until Ajax Call Has Got Data Before Displaying
Get Request Works With Postman But Why Doesn't It Work With Reactjs Fetch
Possible to Detect If a User Has Multiple Tabs of Your Site Open
Generate Child Row Under a Parent Row in HTML Table
Replace Multiple Strings With Multiple Other Strings
How to Trigger a Jquery Function Only Once in One Page, Per Session
Handle File Download from Ajax Post
How to Sort Data in Ascending or Descending Order in Reactjs
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player
React Router Dom Returns a Blank Page When Going to a Particular Route
Jquery - Ajax Returning Error 500 on Some Posts
Angular 4 | Window.Scrollto(); Is Not Working Properly
Connecting to Tcp Socket from Browser Using JavaScript
Bootstrap Button Drop-Down Inside Responsive Table Not Visible Because of Scroll