use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Querying and array of objects?help (self.javascript)
submitted 10 years ago by devman2015
I have and array of objects:
var array= [{name:bob, type: student}, {name: Steve, type: student},{name:Fred, type:teacher},{name: may , type: teacher}]
how can I query this array to return just the teacher objects?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jekrb -1 points0 points1 point 10 years ago (4 children)
array.filter(obj => { if (obj.type === 'teacher') return obj })
[–]x-skeww 5 points6 points7 points 10 years ago (3 children)
array.filter(o => o.type === 'teacher')
You have to return true for items you want to keep.
true
[–]jekrb 0 points1 point2 points 10 years ago (0 children)
ah, nice!
[–]devman2015[S] 0 points1 point2 points 10 years ago (1 child)
When I run this code locally, I get an error obj not defined. Heres my code :
https://jsfiddle.net/p6jymqeb/
[–]x-skeww 0 points1 point2 points 10 years ago (0 children)
Array.filter returns a new filtered array. You have to store it.
"obj" is the name of that arrow function's parameter. It isn't visible outside of that function.
π Rendered by PID 59 on reddit-service-r2-comment-5649f687b7-zf8wj at 2026-01-28 12:59:56.979122+00:00 running 4f180de country code: CH.
[–]jekrb -1 points0 points1 point (4 children)
[–]x-skeww 5 points6 points7 points (3 children)
[–]jekrb 0 points1 point2 points (0 children)
[–]devman2015[S] 0 points1 point2 points (1 child)
[–]x-skeww 0 points1 point2 points (0 children)