Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic Manipulation of Arrays to Remove an Index

I am trying to create a function that can manipulate arrays...

 var myArray = [2, 1, 1, 1, 1];

now I want it to be like this

[3, 1, 1, 1]

now my function accepts 3 parameters

  1. ArrayToProcess - well its the array that will be processed
  2. indexTarget - this is the selected value that is defined by index
  3. morphToValue - this is the value I want the selected value to become.

well my objective is to accept the indexTarget which for example

myFunction(myArray,0,3); //myArray is [2, 1, 1, 1, 1]

as you can see I want my function to loop over the myArray so it will try to add numbers in the array until it will reach the morphToValue so it became [3, 1, 1, 1], it purges the 2 on the first index and 1 on the second index to gain 3. It will just also minus any number on the array if it adds too much on the exceeding on the morphToValue

another example will be like this I want the array

var myArray = [2, 1, 1, 1, 1];

to be like this

[2, 1, 3];

by doing call myFunction like this

myFunction(myArray,2,3);

how can I make this possible? I want also to continue iterating on the beginning again of the array if I will set indexTarget in the last index of the array so it would be like this

var myArray = [2, 1, 1, 1, 1];

it will become

[1, 1, 1, 3]; //when I invoke myFunction(myArray,4,3);

please put a comment if you don't understand something....

this is what I have tried so far http://jsfiddle.net/eESNj/

var myArray = ['2', '1', '1', '1', '1'];

indexPurge(myArray, 0, 3);

function indexPurge(haystack, indexTarget, morphToValue) {

    var toIntHaystack = [];

    for (var i = 0; i < haystack.length; i++) {
        toIntHaystack.push(parseInt(haystack[i]));
    }

    console.log(toIntHaystack); //before

    var i = 0;

    var purgedValue = 0;

    do {
        console.log(i + ' - ' + toIntHaystack[i]);
        purgedValue += toIntHaystack[i];
        toIntHaystack.splice(i, 1);
        if (purgedValue >= morphToValue) {
            break;
        }
        i++;
    } while (i < toIntHaystack.length);



    toIntHaystack.splice(indexTarget, 0, morphToValue); //after
    console.log(toIntHaystack);

}
like image 987
Netorica Avatar asked Oct 25 '13 03:10

Netorica


Video Answer


2 Answers

here is my variant, it should work faster then splice variant:

function fixIdx(idx, len) {
    return idx % len; 
}
function fixStartOffset(idx, len) {
    return (idx < len) ? 0 : (idx % len);
}
function morhArray(inputArray, startIndex, targetValue) {
    var l = inputArray.length;
    var sum = 0;
    var endIdx = 0;
    while ( (sum < targetValue) && (endIdx < l) )
        sum += inputArray[fixIdx(startIndex+endIdx++, l)];
    if (endIdx == l) return [sum];
    var outputArray = [];
    for (var i=fixStartOffset(startIndex+endIdx, l); i<startIndex; i++)
        outputArray.push(inputArray[i]);
    outputArray.push(sum);
    for (var i=startIndex+endIdx; i<l; i++)
        outputArray.push(inputArray[i]);
    return outputArray;
}
like image 74
Iłya Bursov Avatar answered Oct 12 '22 23:10

Iłya Bursov


I solved my own problem.. I hope this will help someone that wil have the same problem

http://jsfiddle.net/YUdJL/

var myarrayx =  [1,1,3]; //5

function morphArray(myarray, index, target){

    //accumulate value on the array to gain target value
    var accu = 0;
    for(var i = 0; i < myarray.length; i++){
        var thisIndex = myarray[i];
        for(var j = 0; j < thisIndex; j++){
            if(accu != target){
                myarray[i]--;  
                accu++;
            }
            else{
                break;
            }
        }
    }

    //remove the zeroes on the array
    for(var k = 0; k < myarray.length; k++){
        if(myarray[k] == 0){
            myarray.splice(k, 1);
        }
    }

    //check if the target index is still available
    if((myarray.length - 1) > index){
        //index is not available anymore.. just push it
        myarray.push(target);
    }
    else{
        //insert the element in the desired index
        myarray.splice(index, 0, target);
    }


    return myarray;
}

console.log('----'+morphArray(myarrayx, 0, 2));
like image 31
Netorica Avatar answered Oct 13 '22 00:10

Netorica