Qiqke
Qiqke

Reputation: 486

Why are the values of a js array overwriting themselves?

I have a problem when I try to assign values to a js array, I mean, I can check that I'm iterating all the elements but when I assign the value the size where I place that value doesn't change... This is the simplified html:

<div id="HotelAccordion0">
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
               <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse1" class="" aria-expanded="true">

               <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Standard -  <span class="precioMasBajo">Desde <strong>50,00 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse2" class="collapsed" aria-expanded="false">
                    <i class="fa fa-lg fa-angle-down pull-right"></i>
                    <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Vista Jardin -  <span class="precioMasBajo">Desde <strong>70,00 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse3" class="collapsed" aria-expanded="false">
                    <i class="fa fa-lg fa-angle-down pull-right"></i>
                    <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Doble + Supletoria adulto -  <span class="precioMasBajo">Desde <strong>82,80 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
</div>

This is 1 object, then, for example, I have another one:

<div class="panel-group smart-accordion-default" id="HotelAccordion1">
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse1" class="" aria-expanded="true">

                    <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Standard -  <span class="precioMasBajo">Desde <strong>40,00 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse2" class="collapsed" aria-expanded="false">
                    <i class="fa fa-lg fa-angle-down pull-right"></i>
                    <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Vista Mar-  <span class="precioMasBajo">Desde <strong>200,00 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
    <div class="panel panel-default">
        <div class="panel-heading" id="cabeceraTarifas">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#HotelAccordion0" href="#collapse3" class="collapsed" aria-expanded="false">
                    <i class="fa fa-lg fa-angle-down pull-right"></i>
                    <i class="fa fa-lg fa-angle-up pull-right"></i>
                    Doble + blablabla -  <span class="precioMasBajo">Desde <strong>90,95 €</strong></span>
                </a>
            </h4>
        </div>

    </div>
</div>

So I need to take the first value, the lowest number (came from the back ordered by minPrice), what I am able to do, but one value is stacking each other in the variable memory(?), this is how I retrieve only the value:

var preciosHotelesArray = [];
var numberPattern = /\d+/g;

for (var i = 0; i < whatever.length; i++) {
        var precioMinimoPorHotel = $("#HotelAccordion" + i + ">.panel.panel-default:first-child").map(function () {
        return this.innerText.match(numberPattern).join(',');
        console.log(precioMinimoPorHotel);
        preciosHotelesArray[i] = this.innerText.match(numberPattern).join(',');
    }).get();    
}

And here all values are stacking each other, whatever I see only last value are inside the array?, What I am doing wrong??

-------WORKING EXAMLE---------

Fiddle link

So now only displays 50. And it should display {0:50, 1:40}.

Thank you very much!!.

Upvotes: 2

Views: 115

Answers (1)

Zamfi
Zamfi

Reputation: 331

You should move the line with return to the end of the function, after adding the value to the array

Upvotes: 1

Related Questions