creating new arrays based on and ordered by one original array in javascript

212 views Asked by At

I am writing javascript to try to do only a few simple tasks.

var series = ["A","0","0","B","0","C","0","0","D","0"];
var off = 1;
var built = 1;
var position = 1;
var masterSeries = [];
var slot = 1;

console.log(series);

function createMaster() {
    for (var i = 0; i <= series.length - 1; ++i) {
        masterSeries[i] = ["Mode" + off];
        //console.log("Creating Internal Array #" + off);
        off++
    }
    off = 1;
    //since we already have first series we just assign it here
    masterSeries[0] = series;
    return masterSeries;
}

function buildAltSeriesNoNull() {
    for (var i = 1; i <= series.length - 1; i++) {
        slot++;
        console.log("Checking Slot: " + slot); 
        if (series[i] != "0") {
            console.log("Found Non Null, building: " + built);
            //code to mutate the original array into new arrays goes here
            var temp = series;
            var back = temp.slice(i, series.length);
            var front = temp.slice(0,i);
            var newline = back.concat(front);
            masterSeries[position] = newline;
            position++;
   console.log("Added to Master Series Mini Array:" + position);
            built++;
            
        } else {
            console.log("Skipping Slot: " + slot);
            
        }
        off++;
        //masterSeries[i] = ["Love" + numOffset];  //set the mode
    }
console.log(masterSeries);
}
console.log(createMaster()); //Create the MasterSeries Display the Banks
console.log("Memory banks have been created, and we have a valid series of numbers to work with!");
console.log('\n');
console.log(buildAltSeriesNoNull());

  1. I have an hard coded array of 10 index's that hold possible letters
  2. there will be a random amount of letters populating the 10 slots, it should not matter if the first index is null or not null

For Example

A00B0C00D0

ABC00D00EF

0A0B0C0D0E

  1. The zero's should be treated as null (will come in handy in a second)
  2. First I want the program to iterate through each index after the first and

    A. determine if it is a null or a valid letter

    B. If it is null, then skip to next index

    C. If it has a valid letter than it will create a new array and 'resort' the original array into a custom sorted array that looks like this. (using one of the example original arrays above)

ORIGINAL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->A00B0C00D0

Program checks index 2, and it is null, move to next, checks index 3 it is null, move to next. Index 4 has a value, "B" So now program creates a new array simply called array2nditerate and the array now looks like this

SECOND ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->B0C00D0A00

THIRD ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->C00D0A00B0

FOURTH ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->D0A00B0C00

So it is creating a new array for each unique letter based on the position in the original array it sat.

So once It creates all the unique sorted arrays for each slot that had a value. I would then need it to do the entire same process but this time to the positions in the original array that had null values only...so for example it would look like this.

ORIGINAL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->A00B0C00D0

FIRST NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->00B0C00D0A

SECOND NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->0B0C00D0A0

THIRD NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->0C00D0A00B

FOURTH NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->00D0A00B0C

FIFTH NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->0D0A00B0C0

SIXTH NULL ARRAY

index-->[0,1,2,3,4,5,6,7,8,9]

Value-->0A00B0C00D

If you notice it created 4 Non Null Arrays custom sorted because there were only 4 letters in the array of 10 possible index positions. it created six non nulls because 10 positions -4 non null arrays is 6 null arrays

I am not sure as to which method is quicker, better. Iterating with one for loop and sorting out into a pile of null arrays and sorting into a pile of non null arrays, or writing two separate functions that iterate

  1. once through looking only for non null index's and sorting that way
  2. sorting through looking for null index's and sorting that way
1

There are 1 answers

0
Nina Scholz On

This is based on the thought of maintaining the original array and works only with two abstraction, the offset of the found letters and zeros.

I think, a simple iteration should sort out zero and letter indices.

this.array.forEach(function (a, i) {
    a === '0' ? that.indexZero.push(i) : that.indexLetter.push(i);
});

Working example:

function ClassWithoutName(data) {
    var that = this;
    this.array = data.split(''),
    this.indexLetter = [],
    this.indexZero = [];

    this.array.forEach(function (a, i) {
        a === '0' ? that.indexZero.push(i) : that.indexLetter.push(i);
    });
}

ClassWithoutName.prototype = {
    constructor: ClassWithoutName,

    getItem: function (index) {
        return this.array[index % this.array.length];
    },

    getArray: function (offset) {
        offset = offset || 0;
        return this.array.map(function (_, i, o) {
            return o[(i + offset) % o.length];
        });
    }
};

var instanceWithoutName = new ClassWithoutName('A00B0C00D0');

console.log('data: ' + instanceWithoutName.getArray().join(''));

console.log('letters:');
instanceWithoutName.indexLetter.forEach(function (a, i) {
    console.log(i + ': offset: ' + a + ' ' + instanceWithoutName.getArray(a).join(''));
});

console.log('zeros:');
instanceWithoutName.indexZero.forEach(function (a, i) {
    console.log(i + ': offset: ' + a + ' ' + instanceWithoutName.getArray(a).join(''));
});

console.log('special selected item, here 2nd abstraction of letter element 3:');
console.log(instanceWithoutName.getItem(instanceWithoutName.indexLetter[2] + 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }