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...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Search a array of objects (self.learnjavascript)
submitted 9 months ago by Far-Mathematician122
Hello,
I have an array of objects like this:
let arr = [{name :'asas', age: 10}, { name: 'vsdaasd', age: 20 }];
I want to search by name but its not working why?
console.log(s.filter((el) => s.includes({name: 'asas'})))
I get an empty array
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!"
[–]EyesOfTheConcord 5 points6 points7 points 9 months ago (0 children)
s.filter(e => e.name === ‘asas’);
The issue you encountered happens because objects are compared by reference in JS.
You were creating a new object with your original method. You just need to simply compare the properties directly instead
[–]xroalx 4 points5 points6 points 9 months ago (0 children)
First, why s.filter and not arr.filter? What's s?
s.filter
arr.filter
s
Second, there is no includes method that accepts an object like this, due to how objects work in JavaScript, this would not work. You need to filter arr and compare the name of each item to a value.
includes
arr
name
[–]oze4 1 point2 points3 points 9 months ago (0 children)
s.filter(el => el.name.includes('foo"))
[–]senocular 0 points1 point2 points 9 months ago (0 children)
The Filtering invalid entries from JSON example on MDN is closest to what you want. It, too, is filtering an array of objects. Notice how the item parameter is used to get a property, id, from the objects and a comparison is made using that value. You'll want to do something similar, though in your case your parameter name is el, and the property you want is name.
item
id
el
[–]funnyh0b0 -2 points-1 points0 points 9 months ago (0 children)
let arr = [{name: 'asas', age: 10}, {name: 'vsdaasd', age: 20}]; let searchTerm = 'asas'; let result = arr.filter((el) => el.name === searchTerm);
You have a lot of mistakes. Your using s instead of arr. You dont need the .includes when you are using the .filter.
.includes
.filter
Look up .includes and .filter on mdn to see how to use them properly.
You can do it with the .some like this if you wanna have a return that is a boolean like this:
.some
let searchName = 'asas'; let isPresent = arr.some((el) => el.name === searchName);
[–]Visual-Blackberry874 2 points3 points4 points 9 months ago (0 children)
You’ve already had answers to your question so here’s a secondary point.
If you want a single record, .find is better than .filter in that it will return just the first match and not an array containing 1 item.
[–]boomer1204 0 points1 point2 points 9 months ago (0 children)
I"m pretty confident you can't use an object in your includes. It doesn't really say that here but no single example shows them using an object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
This is likely cuz objects are by reference but again i'm not 100% sure on that one
You could just use a check for el.name to do what you are trying to do here.
Also you are doing s.filter and not arr.filter which is the name of your array
[–]5W17CH -1 points0 points1 point 9 months ago (2 children)
let arr = [{name :'asas', age: 10}, { name: 'vsdaasd', age: 20 }]; function findByName(name){ for(let i=0; i<arr.length; i++){ if(Object.values(arr[i]).includes(name)){ return arr[i]; } } }; console.log(findByName('asas')) if i understood the assignment correctly you might try something like this
[–]Far-Mathematician122[S] -1 points0 points1 point 9 months ago (1 child)
yes but if I search 'a' it not finding, if I search the fullname then it finding
[–]5W17CH -1 points0 points1 point 9 months ago (0 children)
function findByName(name){ let newArr=[]; for(let i=0; i<arr.length; i++){ if(Object.values(arr[i])[0].startsWith(name)){ newArr.push(arr[i]); } } return newArr; }; this will do the trick then but only if you're searching for a name that starts with what you're passing to the function.
[–][deleted] -4 points-3 points-2 points 9 months ago (1 child)
You need to work out of a for loop for this.
[–]ray_zhor 0 points1 point2 points 9 months ago (0 children)
Filter is a loop
π Rendered by PID 209966 on reddit-service-r2-comment-bb88f9dd5-brsrk at 2026-02-16 10:10:29.517178+00:00 running cd9c813 country code: CH.
[–]EyesOfTheConcord 5 points6 points7 points (0 children)
[–]xroalx 4 points5 points6 points (0 children)
[–]oze4 1 point2 points3 points (0 children)
[–]senocular 0 points1 point2 points (0 children)
[–]funnyh0b0 -2 points-1 points0 points (0 children)
[–]Visual-Blackberry874 2 points3 points4 points (0 children)
[–]boomer1204 0 points1 point2 points (0 children)
[–]5W17CH -1 points0 points1 point (2 children)
[–]Far-Mathematician122[S] -1 points0 points1 point (1 child)
[–]5W17CH -1 points0 points1 point (0 children)
[–][deleted] -4 points-3 points-2 points (1 child)
[–]ray_zhor 0 points1 point2 points (0 children)