如何从具有混合值的较大数组返回具有相等值的多个数组?

时间:2021-11-30 12:21:33

I have an array that after being sorted appears like this:

我有一个数组,在排序后显示如下:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];


There are 2 "a" Strings, 4 "b" Strings, and 3 "c" Strings.

I am trying to return 3 separate arrays, returning them one at a time from a loop, containing only matching values. So, upon the first iteration, the returned array would appear as newArr = ["a", "a"],  the second as newArr = ["b", "b", "b", "b"]  and on the third iteration as newArr = ["c", "c", "c"].  

However, this is a small array of predefined values, and I need an algorithm that can perform the same operation on an array of unknown size, unknown elements, and with an unknown number of like elements. (and keep in mind that the array is already sorted to begin with, in this context)

Here's my crazy code that is displaying some unusual, and incorrect, results:

有2个“a”字符串,4个“b”字符串和3个“c”字符串。我试图返回3个独立的数组,从循环中一次返回一个数组,只包含匹配的值。因此,在第一次迭代时,返回的数组将显示为newArr = [“a”,“a”],第二次显示为newArr = [“b”,“b”,“b”,“b”]并且第三次迭代为newArr = [“c”,“c”,“c”]。但是,这是一个小数组的预定义值,我需要一个算法,可以对未知大小的数组,未知元素和未知数量的相似元素执行相同的操作。 (并且请记住,在此上下文中,数组已经开始排序)这是我疯狂的代码,它显示了一些不寻常的,不正确的结果:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];

for(var index = 0; index < arr.length; index++)
{
  var test = "";
  var newArr = []; // resets the new array upon each iteration
  var str = arr[index]; // initialized as the next unique index-value

  for(var i = index; i < arr.length; i++)
  {
    if(arr[i] == str)
    {
      newArr.push(arr[k]);
      test += arr[i] + " ";
    }
    else
    {
      index = i; // changing the outer loop variable
      break; // exiting the inner loop
    }
  } // end of inner loop

  window.alert(test);
  setValues(newArr);

} // end of outer loop

function setValues(arrSorted)
{
  var here = document.getElementById("here");

  for(var i = 0; i < arrSorted.length; i++)
  {
    here.innerHTML += arrSorted[i] + "&nbsp;";
  }

  here.innerHTML += "<br />";

} // end of setValues function

3 个解决方案

#1


2  

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];
var arrays = {};
for (var i=0;i<arr.length;i++) {
  if (!arrays[arr[i]]) arrays[arr[i]] = [];
  arrays[arr[i]].push(arr[i]);
}

this will give you the equivalent of

这将给你相当于

arrays = {};
arrays['a'] = ['a','a'];
arrays['b'] = ['b','b','b','b','b'];
arrays['c'] = ['c','c','c'];

#2


1  

You can use a function like this to divide the array into several arrays:

您可以使用这样的函数将数组划分为多个数组:

function divide(arr) {

  var subArrays = [];
  var current = null;
  var subArray = null;

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != current) {
      if (subArray != null) subArrays.push(subArray);
      current = arr[i];
      subArray = [];
    }
    subArray.push(arr[i]);
  }
  if (subArray != null) subArrays.push(subArray);
  return subArrays;
}

Demo: http://jsfiddle.net/Guffa/d8CBD/

#3


1  

This is how I would do it:

我就是这样做的:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];

var out = [], prev;
for (var i = 0, j = 0, len = arr.length; i < len; i++) {
    if (arr[i] !== prev || !out.length) {
        out[j++] = [prev = arr[i]];
    } else {
        out[j - 1].push(prev);
    }
}

//out -> [["a","a"],["b","b","b"],["c","c","c"]]

Demo

Note: the || !out.length check is just handle arrays that start with undefined correctly, but feel free to remove it if this will never be the case

注意:|| !out.length检查只是正确启动未定义的句柄数组,但如果永远不是这样,请随意删除它

#1


2  

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];
var arrays = {};
for (var i=0;i<arr.length;i++) {
  if (!arrays[arr[i]]) arrays[arr[i]] = [];
  arrays[arr[i]].push(arr[i]);
}

this will give you the equivalent of

这将给你相当于

arrays = {};
arrays['a'] = ['a','a'];
arrays['b'] = ['b','b','b','b','b'];
arrays['c'] = ['c','c','c'];

#2


1  

You can use a function like this to divide the array into several arrays:

您可以使用这样的函数将数组划分为多个数组:

function divide(arr) {

  var subArrays = [];
  var current = null;
  var subArray = null;

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] != current) {
      if (subArray != null) subArrays.push(subArray);
      current = arr[i];
      subArray = [];
    }
    subArray.push(arr[i]);
  }
  if (subArray != null) subArrays.push(subArray);
  return subArrays;
}

Demo: http://jsfiddle.net/Guffa/d8CBD/

#3


1  

This is how I would do it:

我就是这样做的:

var arr = ["a", "a", "b", "b", "b", "b", "c", "c", "c"];

var out = [], prev;
for (var i = 0, j = 0, len = arr.length; i < len; i++) {
    if (arr[i] !== prev || !out.length) {
        out[j++] = [prev = arr[i]];
    } else {
        out[j - 1].push(prev);
    }
}

//out -> [["a","a"],["b","b","b"],["c","c","c"]]

Demo

Note: the || !out.length check is just handle arrays that start with undefined correctly, but feel free to remove it if this will never be the case

注意:|| !out.length检查只是正确启动未定义的句柄数组,但如果永远不是这样,请随意删除它