-2

私はいくつかの単純なタスクだけを実行しようとする JavaScript を書いています。

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. 可能な文字を保持する10個のインデックスのハードコーディングされた配列があります
  2. 10個のスロットにランダムな量の文字が入力されます。最初のインデックスがnullかnullでないかは問題ではありません

例えば

A00B0C00D0

ABC00D00EF

0A0B0C0D0E

  1. ゼロは null として扱われるべきです (すぐに役に立ちます)
  2. 最初に、プログラムが最初のインデックスの後に各インデックスを反復処理するようにします。

    A. null か有効な文字かを判断する

    B. null の場合、次のインデックスにスキップ

    C. 有効な文字がある場合、新しい配列が作成され、元の配列が次のようなカスタムの並べ替えられた配列に「再ソート」されます。(上記の元の配列の例の 1 つを使用)

オリジナルアレイ

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> A00B0C00D0

プログラムはインデックス 2 をチェックし、null の場合は次へ移動し、インデックス 3 をチェックします。null の場合は次へ移動します。インデックス 4 には値 " B" があるため、プログラムは単純に呼び出された新しい配列を作成しarray2nditerate、配列は次のようになります

2 番目の配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> B0C00D0A00

3 番目のアレイ

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 -->C00D0A00B0

4番目のアレイ

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> D0A00B0C00

そのため、元の配列内の位置に基づいて、一意の文字ごとに新しい配列を作成しています。

そのため、一度、値を持つスロットごとに一意の並べ替えられた配列をすべて作成します。次に、同じプロセス全体を実行する必要がありますが、今回は、null 値のみを持つ元の配列内の位置に対して...たとえば、次のようになります。

オリジナルアレイ

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> A00B0C00D0

最初のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値-->00B0C00D0A

2 番目のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> 0B0C00D0A0

3 番目のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> 0C00D0A00B

4 番目のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値-->00D0A00B0C

5 番目のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> 0D0A00B0C0

6 番目のヌル配列

インデックス -->[0,1,2,3,4,5,6,7,8,9]

値 --> 0A00B0C00D

10 個の可能なインデックス位置の配列に 4 文字しかないため、カスタム ソートされた 4 つの非 Null 配列が作成されたことに気付いた場合。10 の位置 -4 の非ヌル配列は 6 つのヌル配列であるため、6 つの非ヌルを作成しました

どちらの方法がより速く、より良いかはわかりません。1 つの for ループで反復し、null 配列の山に並べ替えてから非 null 配列の山に並べ替える、または反復する 2 つの別個の関数を記述する

  1. null以外のインデックスのみを探し、そのようにソートする
  2. nullインデックスを探してソートし、そのようにソートする
4

1 に答える 1

1

これは、元の配列を維持するという考えに基づいており、見つかった文字とゼロのオフセットという 2 つの抽象化でのみ機能します。

単純な反復でゼロと文字のインデックスを整理する必要があると思います。

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

作業例:

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; }

于 2015-08-15T12:21:47.167 に答える