Mit dem folgenden Code würde er also das aktuelle Ergebnis und die zugehörigen Indizes zurückgeben, in dieser einfachen Situation handelt es sich um Buch A (Index 0) und Buch C (Index 2).
Ich könnte eine einfache For-Schleife für das Objekt verwenden. Das Bücherobjekt ist für einen Buchladen, der viel mehr Titel hat, aber hier eingeschränkt ist.
Code: Select all
function findObjectsByProperty(array, property, value){
return array.filter(obj => obj[property] === value);
}
const books = [
{ id: 1, title: 'Book A', genre: 'Fiction' },
{ id: 2, title: 'Book B', genre: 'Non-Fiction' },
{ id: 3, title: 'Book C', genre: 'Fiction' },
{ id: 4, title: 'Book D', genre: 'Non-Fiction' }
];
const fictionBooks = findObjectsByProperty(books, 'genre', 'Fiction');
Mobile version