How do you split an array into array pairs in Java

2020-02-03 09:10发布

I want to split an array into pairs of arrays

so var arr=[2,3,4,5,6,4,3,5,5]

would be newarr =[[2,3],[4,5],[6,4],[3,5],[5]]

12条回答
够拽才男人
2楼-- · 2020-02-03 09:43

A slightly different approach than using a for loop for comparison. To avoid modifying the original array slice makes a shallow copy since JS passes objects by reference.

function pairArray(a) {
  var temp = a.slice();
  var arr = [];

  while (temp.length) {
    arr.push(temp.splice(0,2));
  }

  return arr;
}

var array = [2,3,4,5,6,4,3,5,5];
var newArr = pairArray(array);

function pairArray(a) {
  var temp = a.slice();
  var arr = [];

  while (temp.length) {
    arr.push(temp.splice(0,2));
  }

  return arr;
}

document.write('<pre>' + JSON.stringify(newArr) + '</pre>');

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-03 09:45

const items = [1,2,3,4,5];

const createBucket = (bucketItems, bucketSize) => buckets => { return bucketItems.length === 0 ? buckets : [...buckets, bucketItems.splice(0, bucketSize)]; };

const bucketWithItems = items.reduce(createBucket([...items], 4), []);

查看更多
做个烂人
4楼-- · 2020-02-03 09:49

I would use lodash for situations like this.

Here is a solution using _.reduce:

var newArr = _(arr).reduce(function(result, value, index) {
  if (index % 2 === 0)
    result.push(arr.slice(index, index + 2));

  return result;
}, []);

var arr = [2,3,4,5,6,4,3,5,5];

var newArr = _(arr).reduce(function(result, value, index) {
  if (index % 2 === 0)
    result.push(arr.slice(index, index + 2));
  
  return result;
}, []);

document.write(JSON.stringify(newArr)); // [[2,3],[4,5],[6,4],[3,5],[5]]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>

查看更多
戒情不戒烟
5楼-- · 2020-02-03 09:52

Here's a good generic solution:

function splitInto(array, size, inplace) {
    var output, i, group;

    if (inplace) {
        output = array;

        for (i = 0; i < array.length; i++) {
            group = array.splice(i, size);

            output.splice(i, 0, group);
        }
    } else {
        output = [];

        for (i = 0; i < array.length; i += size) {
            output.push(array.slice(i, size + i));
        }
    }

    return output;
}

For your case, you can call it like this:

var arr= [2,3,4,5,6,4,3,5,5];
var newarr = splitInto(arr, 2);

The inplace argument determines whether the operation is done in-place or not.

Here's a demo below:

function splitInto(array, size, inplace) {
    var output, i, group;

    if (inplace) {
        output = array;

        for (i = 0; i < array.length; i++) {
            group = array.splice(i, size);

            output.splice(i, 0, group);
        }
    } else {
        output = [];

        for (i = 0; i < array.length; i += size) {
            output.push(array.slice(i, size + i));
        }
    }

    return output;
}

var arr= [2,3,4,5,6,4,3,5,5];
var newarr = splitInto(arr, 2);

disp(newarr);

// or we can do it in-place...
splitInto(arr, 3, true);

disp(arr);

function disp(array) {  
  var json = JSON.stringify(array);

  var text = document.createTextNode(json);
  var pre = document.createElement('pre');

  pre.appendChild(text);
  document.body.appendChild(pre);
}

查看更多
甜甜的少女心
6楼-- · 2020-02-03 09:56

It's possible to group an array into pairs/chunks in one line without libraries:

function chunks(arr, size = 2) {
  return arr.map((x, i) => i % size == 0 && arr.slice(i, i + size)).filter(x => x)
}
console.log(chunks([1, 2, 3, 4, 5, 6, 7])) // -> [[1, 2], [3, 4], [5, 6], [7]]

查看更多
仙女界的扛把子
7楼-- · 2020-02-03 10:02

Lodash has a method for this: https://lodash.com/docs/4.17.10#chunk

_.chunk([2,3,4,5,6,4,3,5,5], 2); // => [[2,3],[4,5],[6,4],[3,5],[5]]

查看更多
登录 后发表回答