I'm trying to search in an array and find specific objects based on their category.
But when I run my code, I only get one result. but in the example below, I should have two results because the cat 2
exists in two of the objects!
This is what I have:
var storedArray = [{
"title": "test title 1",
"date_added": "2018-09-26",
"url": "someurl",
"filename": "file 1",
"category": "cat 1"
}, {
"title": "test title 2",
"date_added": "2018-10-25",
"url": "someurl",
"filename": "file 2",
"category": "cat 2"
},{
"title": "test title 3",
"date_added": "2018-10-25",
"url": "someurl",
"filename": "file 3",
"category": "cat 2"
}];
var result = storedArray.find( audio => audio.category === 'cat 2' );
console.log(result);
I'm trying to search in an array and find specific objects based on their category.
But when I run my code, I only get one result. but in the example below, I should have two results because the cat 2
exists in two of the objects!
This is what I have:
var storedArray = [{
"title": "test title 1",
"date_added": "2018-09-26",
"url": "someurl.com",
"filename": "file 1",
"category": "cat 1"
}, {
"title": "test title 2",
"date_added": "2018-10-25",
"url": "someurl.com",
"filename": "file 2",
"category": "cat 2"
},{
"title": "test title 3",
"date_added": "2018-10-25",
"url": "someurl.com",
"filename": "file 3",
"category": "cat 2"
}];
var result = storedArray.find( audio => audio.category === 'cat 2' );
console.log(result);
Could someone please advice on this issue?
Share Improve this question edited Oct 2, 2018 at 14:37 BryanH 6,0623 gold badges36 silver badges47 bronze badges asked Oct 2, 2018 at 13:10 James JuanjieJames Juanjie 2194 silver badges21 bronze badges 2- you don't need jquery! – Anouar Commented Oct 2, 2018 at 13:12
- "The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned." MDN – epascarello Commented Oct 2, 2018 at 13:16
5 Answers
Reset to default 10Array.prototype.find()
The
find()
method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
Array.prototype.filter()
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
From the above, you have to use filter()
to get the expected result:
var storedArray = [{
"title": "test title 1",
"date_added": "2018-09-26",
"url": "someurl.com",
"filename": "file 1",
"category": "cat 1"
}, {
"title": "test title 2",
"date_added": "2018-10-25",
"url": "someurl.com",
"filename": "file 2",
"category": "cat 2"
},{
"title": "test title 3",
"date_added": "2018-10-25",
"url": "someurl.com",
"filename": "file 3",
"category": "cat 2"
}];
var result = storedArray.filter( audio => audio.category === 'cat 2' );
console.log(result);
Array.prototype.find() returns the first element found. You are looking for Array.prototype.filter()
You want to use filter function.
From the MDN documentation on find...
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
Key word: first.
The method you are using .find()
is returning only the first record that matches the condition, this is why you are getting only 1 result.