Q:

How can I overwrite my array on window resize?

I have multiple div's with different content and same class
<div class="my-div"> ... some content here ... </div>
<div class="my-div"> ... some content here ... </div>
<div class="my-div"> ... some content here ... </div>
<div class="my-div"> ... some content here ... </div>
<div class="my-div"> ... some content here ... </div>

Now I push each individual height into an array
var myArray = new Array();

function pushToArray(height) {
    myArray.push(height);
}

function getHeight() {
    $('.my-div').each(function () {
        pushToArray($(this).height());
    });
}

This returns each individual height at document loaded. If I know call this function chain on window re-size, it will extend my array. What I want is that it replaces the existing values with the new heights.
How do I achieve that?

A:

Reset the array in getHeight function.
var myArray = [];

function pushToArray(height) {
    myArray.push(height);
}

function getHeight() {
    // Reinitialize the array
    myArray = [];

    $('.my-div').each(function () {
        pushToArray($(this).height());
    });
}

// Call getHeight on window resize
$(window).resize(getHeight);

