【问题标题】:creating new arrays based on and ordered by one original array in javascript基于javascript中的一个原始数组创建新数组并按其排序
【发布时间】:2015-08-15 01:03:59
【问题描述】:

我正在编写 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 无关紧要

举例

A00B0C00D0

ABC00D00EF

0A0B0C0D0E

  1. 应将零视为空值(稍后会派上用场)
  2. 首先我希望程序在第一个和之后遍历每个索引

    A.判断是空还是有效的字母

    B.如果为null,则跳到下一个索引

    C.如果它有一个有效的字母,它将创建一个新数组并将原始数组“重新排列”为一个看起来像这样的自定义排序数组。 (使用上面的示例原始数组之一)

原始数组

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

值-->A00B0C00D0

程序检查索引 2 是否为空,移动到下一个,检查索引 3 是否为空,移动到下一个。索引 4 有一个值“B”所以现在程序创建了一个简单称为array2nditerate 的新数组,该数组现在看起来像这样

第二个数组

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

值-->B0C00D0A00

第三阵列

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

值-->C00D0A00B0

第四阵

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

值-->D0A00B0C00

所以它会根据它在原始数组中的位置为每个唯一字母创建一个新数组。

所以一旦它为每个有值的槽创建了所有唯一的排序数组。然后我需要它来执行整个相同的过程,但这次是原始数组中只有空值的位置......所以例如它看起来像这样。

原始数组

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

值-->A00B0C00D0

第一个空数组

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

值-->00B0C00D0A

第二个空数组

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

值-->0B0C00D0A0

第三个空数组

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

值-->0C00D0A00B

第四个空数组

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

值-->00D0A00B0C

第五个空数组

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

值-->0D0A00B0C0

第六个空数组

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

值-->0A00B0C00D

如果您注意到它创建了 4 个自定义排序的非空数组,因为在 10 个可能的索引位置的数组中只有 4 个字母。它创建了六个非空值,因为 10 个位置 -4 个非空数组是 6 个空数组

我不确定哪种方法更快、更好。用一个for循环进行迭代,整理成一堆空数组,再整理成一堆非空数组,或者写两个独立的函数进行迭代

  1. 只查找非空索引并以这种方式排序
  2. 通过查找空索引并以此方式进行排序

【问题讨论】:

  • 那么,您至少要自己尝试一下吗?或者这是一个流行测验? stackoverflow.com/questions/3010840/…
  • 大声笑,对不起@WhiteHat 我确实说过我是菜鸟,所以你认为我应该有一个 for 循环,并在条件部分检查一个值是真还是假?像这样
  • 欢迎来到 StackOverflow。显示一些代码! :)
  • 欢迎来到 Stack Overflow!我进行了编辑以使其更具可读性。请添加您迄今为止尝试过的任何代码,并通过简化问题和减少示例来减少文本墙。无需多谢。在这里点赞和接受答案是感谢他人的方式。
  • 好的,现在正在处理一些代码...请给我一个小时或更短的时间,我愿意支付帮助虽然 wink wink 如果有人要帮我定制这个应用程序,只是提醒一下

标签: javascript arrays sorting for-loop iterated-function


【解决方案1】:

这是基于维护原始数组的思想,只适用于两个抽象,找到的字母和零的偏移量。

我认为,一个简单的迭代应该整理出零和字母索引。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2017-06-24
    • 2017-09-12
    • 1970-01-01
    相关资源
    最近更新 更多