Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 定义数组中的间隔_Javascript_Jquery - Fatal编程技术网

Javascript 定义数组中的间隔

Javascript 定义数组中的间隔,javascript,jquery,Javascript,Jquery,我有一个数组,有几个相等的值 [1,1,1,1,2,3,4,5,5,5,1,1,1,2,2,2,4,5,5,9,9.9] 我想要一种通过分离相等值来获得新数组的方法 例如,新数组将具有值 [1,1,1,1] [5,5,5,5] [1,1,1,1] [2,2,2,2] [5,5,5] [9,9,9,9] 对于这些新数组,我必须在项目更改时找到索引 这就是我到目前为止所尝试的 indices = []; // fill with information when items in array cha

我有一个数组,有几个相等的值

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

我想要一种通过分离相等值来获得新数组的方法

例如,新数组将具有值 [1,1,1,1] [5,5,5,5] [1,1,1,1] [2,2,2,2] [5,5,5] [9,9,9,9]

对于这些新数组,我必须在项目更改时找到索引

这就是我到目前为止所尝试的

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);
            }
索引=[];//当数组中的项发生更改时填充信息
arreglo=[1,1,1,2,3,4,5,5,5,5,1,1,1,2,2,4,5,5,5,9,9];
对于(u=0;uA溶液:

//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);
//结果数组,包含其他数组
var数组_map={};
var arreglo=[1,1,1,1,2,3,4,5,5,5,5,1,1,1,2,2,2,4,5,5,5,9,9];
对于(u=0;uA溶液:

//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);
//结果数组,包含其他数组
var数组_map={};
var arreglo=[1,1,1,1,2,3,4,5,5,5,5,1,1,1,2,2,2,4,5,5,5,9,9];
对于(u=0;u

var prev = false,
    indeces = [];

for(var i=0; i<arreglo.length; i++){
    if(arreglo[i] !== prev){
        prev = arreglo[i];
        indices.push(i);
    }
}
var prev=false,
指数=[];
对于(var i=0;i

var prev = false,
    indeces = [];

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

对于(var i=0;i我认为在以前的版本中缺少数组的类型 版本如下

此外,我想尽可能地减少条件

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 = [];
    }
}
var arreglo=[1,1,1,1,2,3,4,5,5,5,1,1,1,1,2,2,4,5,5,5,5,5,1,1,2,2,4,5,5,5,5,9,9.9];
//首先,对数组进行排序
arreglo.sort();
变量输出阵列=[];
var arrLength=arreglo.length;
var tmpArray=[]

对于(var i=1;i我认为在以前的版本中缺少数组的类型 版本如下

此外,我想尽可能地减少条件

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 = [];
    }
}
var arreglo=[1,1,1,1,2,3,4,5,5,5,1,1,1,1,2,2,4,5,5,5,5,5,1,1,2,2,4,5,5,5,5,9,9.9];
//首先,对数组进行排序
arreglo.sort();
变量输出阵列=[];
var arrLength=arreglo.length;
var tmpArray=[]

对于(var i=1;如果我听起来很恶心和可疑,我原谅我,但这是(1)作业和/或(2)面试(或求职筛选或…)问题吗?如果不是,我很想知道它需要什么。根据你对问题的描述,你的结果不是:[[1,1,1,1,1,1,1],[5,5,5,5],[2,2,2],[9,9,9]?“不起作用”从来都不是一个足够的问题描述。你期望的结果到底是什么,与实际产生的结果有何不同?哎呀。我在看到这些评论之前已经给出了答案。保留它是否正确?@Madbroks我很确定这只是一个问题。如果我听起来很讨厌和可疑,请原谅,但这是(1)吗家庭作业和/或(2)面试(或工作申请筛选或…)问题?如果不是,我很想知道它需要什么。根据你对问题的描述,你的结果是不是:[[1,1,1,1,1,1,1,1,1,1],[5,5,5,5],[2,2,2,2,2],[9,9,9]?“不起作用?”"从来都不是一个足够的问题描述。你期望的结果到底是什么,与实际产生的结果有何不同?哎呀。我在看到这些评论之前已经给出了答案。保留它正确吗?@Madbreaks我很确定这只是一个问题。我也会这样做。希望没问题,我已经为代码写了一些评论。下面是y键是使用原始数组
arreglo
(这里称为
item
)中的数字作为结果数组
array\u map[item]
中的键。然后您只需将该项+1添加到数组中。结果将保存正确排序的数组。此外,您还可以避免使用
if(arreglo[u]!=arreglo[u+1]之类的内容
对于最后一个迭代步骤来说非常难看(其中
arreglo[arreglo.length+1]
未定义:C将以同样的方式执行。希望没问题,我已经为代码写了一些注释。它们的关键是使用原始数组
arreglo
(此处称为
)中的数字作为结果数组
array\u映射中的键[item]
。然后您只需将此项+1添加到数组中。结果将保存正确排序的数组。此外,您还可以避免使用
if(arreglo[u]!=arreglo[u+1])
之类的内容,因为在最后一个迭代步骤中,
arreglo[arreglo.length+1]
是未定义的:C