Is it possible to do without forEach in this example: There is an array of the form

{ count: 0, models: [ { name: 'ACE', url: 'ace', count: 0 }, { name: 'Cobra', url: 'cobra', count: 0 } ], } 

You need to get the value of the "url" field knowing the name of the model "ACE" or "Cobra"

In the case of the forEach solution

 vehicle.models.forEach(function(cell, index) { if(cell.name === ads.car.model.name) { ads.car.model.url = cell.url; } }); 

Maybe there are alternatives?

    1 answer 1

    With such a structure, there is no alternative.

    Try to change the object:

     { count: 0, models: { 'ACE' : { url: 'ace', count: 0 }, 'Cobra' : { url: 'cobra', count: 0 } } } 

    Then you can check without a loop:

     var _modelName = 'ACE'; if(vehicle.models.hasOwnProperty(_modelName) ..... 
    • this data is their base = (I wouldn’t like to edit, but I'll think about it. ATP! - webphp
    • Those. Is the data stored in JSON in the text field? - dekameron
    • yes, in MongoDB - webphp
    • And is it possible to save the string in the variant I proposed at the time of making a record in the database? - dekameron
    • the base is simply already formed, the project is still being written of course. - webphp 10:26 pm