Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript Searching for Index number of Array

I would be very grateful if you could help me. I’ve been sitting here for 5 hours and can’t find a solution. I want to get the number in the array based on the name of the array (ex. "Apfelkuchen"). In this case, "Apfelkuchen" would be number 0.

I know that the indexOF method is used for this, but unfortunately I don’t know. How to use it, because my array is not the usual case.

let Essen = [

    { name: "Apfelkuchen", ingredients: ["Apfel", "Mehl", "Zucker"] },
    { name: "Bananenkuchen", ingredients: ["Banane", "Mehl", "Zucker"] },
    { name: "Käsekuchen", ingredients: ["Köse", "Mehl", "Zucker"] },
    { name: "Steuselkuchen", ingredients: ["Steuselkuchen", "Mehl", "Zucker"] },
    { name: "Schokokuchen", ingredients: ["Schoko", "Mehl", "Zucker"] },
    { name: "Kürbiskuchen", ingredients: ["Kürbis", "Mehl", "Zucker"] },
    { name: "Zitronenkuchen", ingredients: ["Zitronen", "Mehl", "Zucker"] },
    { name: "Erdbeerkuchen", ingredients: ["Erdbeere", "Mehl", "Zucker"] },
    { name: "Nusskuchen", ingredients: ["Nuss", "Mehl", "Zucker"] },
    { name: "Butterkuchen", ingredients: ["Butter", "Mehl", "Zucker"] },

];

function createEssen(_name, _ingredients) {
    Essen.push({name: _name, ingredients: _ingredients})
}

function addIngredients(_name) {
    Essen[10].ingredients.push(_name)
}

function searchArrayNumber(_name) {
    
How????

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use .findIndex():

let Essen = [

    { name: "Apfelkuchen", ingredients: ["Apfel", "Mehl", "Zucker"] },
    { name: "Bananenkuchen", ingredients: ["Banane", "Mehl", "Zucker"] },
    { name: "Käsekuchen", ingredients: ["Köse", "Mehl", "Zucker"] },
    { name: "Steuselkuchen", ingredients: ["Steuselkuchen", "Mehl", "Zucker"] },
    { name: "Schokokuchen", ingredients: ["Schoko", "Mehl", "Zucker"] },
    { name: "Kürbiskuchen", ingredients: ["Kürbis", "Mehl", "Zucker"] },
    { name: "Zitronenkuchen", ingredients: ["Zitronen", "Mehl", "Zucker"] },
    { name: "Erdbeerkuchen", ingredients: ["Erdbeere", "Mehl", "Zucker"] },
    { name: "Nusskuchen", ingredients: ["Nuss", "Mehl", "Zucker"] },
    { name: "Butterkuchen", ingredients: ["Butter", "Mehl", "Zucker"] },

];


console.log(Essen.findIndex(x => x.name == "Apfelkuchen"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading