chepe263
chepe263

Reputation: 2812

Define intervals in an array

I have an array with several equal values

[1,1,1,1,2,3,4,5,5,5,5,1,1,1,1,2,2,2,2,4,5,5,5,9,9,9.9]

I would like a way to get a new array by separating equal values.

For example, the new arrays would have values [1,1,1,1] [5,5,5,5] [1,1,1,1] [2,2,2,2] [5,5,5] [9,9,9,9]

For those new arrays,I must find the index when items changes.

This is what i've tried so far

indices = []; // fill with information when items in array change
arreglo = [1,1,1,1,2,3,4,5,5,5,5,1,1,1,1,2,2,2,2,4,5,5,5,9,9,9.9];
for ( u=0; u <= arreglo.length; u++){
            if ( arreglo[u] !=  arreglo[u + 1])
            indices.push(u);
            }

The idea is finding the index of the biggest array and then loop in it to create the new ones.

Using a loop i would go from 0 to indices[0], later from indices[0] to indices[1] and so on.

It does not work properly, have problems. Is there any efficient way to do this?

Update: This is not homework, it's for a site for a client. I made a question before Need ideas: Selecting rows in table but couldnt make it with mysql so i decided using jQuery

{ This is the real code i'm using http://jsfiddle.net/U58jh/

In the jsfiddle example this is working good but not always when using different data from a php generated page.

The script must find the last dates (fecha) with a final percent (Porcentaje final) iquals. }

Upvotes: 1

Views: 2382

Answers (4)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

A solution :

//the result array, holding other arrays
var array_map = {};

var arreglo = [1,1,1,1,2,3,4,5,5,5,5,1,1,1,1,2,2,2,2,4,5,5,5,9,9,9.9];

for ( u=0; u <= arreglo.length; u++){

        //grab a number from the input array
        var item = arreglo[u];

        //get an object from array_map
        var indices = array_map[item];

        //if the object does not exist ...
        if (!indices) {
            indices = []; // ... create it ^^ ....
            array_map[item] = indices; //... and store it in the result.
        }

        //push the number into the object
        indices.push(item);
}

console.log(array_map);

An error you have : u in your loop makes you iterates on indexes, not values.

Upvotes: 1

Gregory Nozik
Gregory Nozik

Reputation: 3374

I think that in previous versions missing sort of the array. So my version is below.

In addition I wanted to reduce IF conditions as possible.

var arreglo = [1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 1, 1, 1, 1, 2, 2, 2, 2, 4, 5, 5, 5, 9, 9, 9.9];
// First of all sort the array
arreglo.sort();
var outPutArray = [];
var arrLength = arreglo.length;
var tmpArray = []

for (var i = 1; i <= arrLength; i++) {
var tmpValue = arreglo[i];
var previousValue  =arreglo[i-1];

tmpArray.push(previousValue)

if (tmpValue != previousValue) {
        outPutArray.push(tmpArray);

        // The values are differents, so empty temp array
        tmpArray = [];
    }
}

Upvotes: 0

raddrick
raddrick

Reputation: 4453

indices = []; // fill with information when items in array change
arreglo = [1,1,1,1,2,3,4,5,5,5,5,1,1,1,1,2,2,2,2,4,5,5,5,9,9,9,9];

array[0]=[]

for(var i = 0 ; i < arreglo.length ; i++ ){
 var value = arreglo[i]
 if (typeof(array[value]) == "undefined"){
  array[0].push(array[value])
  array[value]=[array[value]]
 }else{
  array[value].push(array[value])
 }
}

this will give you a list of hash locations that have your 'buckets'. array[0] is your list of locations that you can iterate through, and then at each one you have your bucket of values..so

array[0][0] == 1
array[1] = [1,1,1,1,1,1,1,1]
array[0][1] == 2
array[2] = [2,2,2,2,2,2]

Upvotes: 0

Madbreaks
Madbreaks

Reputation: 19539

As for the indices:

var prev = false,
    indeces = [];

for(var i=0; i<arreglo.length; i++){
    if(arreglo[i] !== prev){
        prev = arreglo[i];
        indices.push(i);
    }
}

...however, this will not create your separate arrays (but I don't see that you're attempting to do so in your code either).

Upvotes: 0

Related Questions