Extract "N" Values from an Array

jonplaca

OBJECTIVE

"Chunky Monkey" Problem: Create a new, multi-dimensional array from a given array ('arr') and number ('size'). The new, multi-dimensional array should create array lengths equal to the 'size' variable.

EXAMPLE #1

chunk(['a', 'b', 'c', 'd'], 2);
var answer = [];
function chunk(arr,size){
//do something
};

return answer

EXPECTED ANSWER #1

answer = [['a','b'],['c','d']];

EXAMPLE #2

Below is an example when there is an odd number of elements/sizes - In a situation like this, the expected result is to have a "remainder" group.

chunk(['a', 'b', 'c', 'd'], 3);
    var answer = [];
    function chunk(arr,size){
    //do something
    };

    return answer

EXPECTED ANSWER #2

answer = [['a','b','c'],['d']];


MY PSEUDO-CODE / APPROACH

var final = [];
function chunk(arr, size) {
  //extract number of elements from 'arr' = 'size' 
  var extract = function{
    //HELP! [INSERT LOGIC]
  };

  //add extracted elements to 'group' array
  var group = [extract];

  //push 'group' to 'final' array for multidimensional array
  final.push(group);

  //return final'
  return final;
}

chunk(['a', 'b', 'c', 'd'], 2);

QUESTIONS

  1. I've been looking @ .forEach(), for(in) loops, .slice(), etc. but to no avail. All of my approaches will result in cycling through the array, but not "pairing them".
  2. Am I over-complicating things? Is there a more direct approach than my pseudo-code/approach?
Oriol

You can use a recursive function:

function chunk(arr, size) {
    if(!arr.length) return [];
    return [arr.slice(0, size)].concat(chunk(arr.slice(size), size));
}

Or a loop:

function chunk(arr, size) {
    arr = arr.slice();
    var ret = [];
    while(arr.length) ret.push(arr.splice(0, size));
    return ret;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract "N" Values from an Array

From Dev

PHP extract values from array

From Dev

Extract values from JSON array

From Dev

Extract values from dynamic array

From Dev

extract values from array (php or jquery)

From Dev

Extract values from JSON array using XPath?

From Dev

Extract keys and values from array with wildcard keys

From Dev

Extract values from a JSON Array in Presto

From Dev

Extract keys and values from array with wildcard keys

From Dev

Extract list of duplicate values and locations from array

From Dev

Extract values from JSON array using XPath?

From Dev

How to Extract Values from php array

From Dev

Extract array of key-values from JSON

From Dev

How to extract unique values from an array EXCEPT specified values?

From Dev

Extract array of specific member values from array of class instances in Ruby

From Dev

Python, looking to extract values from a masked array, then rebuild an array

From Dev

Unable to extract values from object array, to which javascript array was deserialized

From Dev

Extract Object Values into an Array

From Dev

How to extract multiple values from a string to call an array?

From Dev

PHP: How to extract() values from an associative array with hyphens/dashes in their keys?

From Java

SQL - Extract values from key value pairs to array

From Java

Extract certain values from XML response to create custom JSON array

From Dev

extract all values of a specific field from a struct array

From Dev

PHP extract values from an array and pass them to a function

From Dev

extract values from multi dimensional array in php (mandrill)

From Dev

Extract values from a list using an array with boolean expressions

From Dev

Extract 14-bit values from an array of bytes in C

From Dev

How to extract number values from an array and then combine it with another string?

From Dev

How to use json array to extract different values from JSON document?

Related Related

  1. 1

    Extract "N" Values from an Array

  2. 2

    PHP extract values from array

  3. 3

    Extract values from JSON array

  4. 4

    Extract values from dynamic array

  5. 5

    extract values from array (php or jquery)

  6. 6

    Extract values from JSON array using XPath?

  7. 7

    Extract keys and values from array with wildcard keys

  8. 8

    Extract values from a JSON Array in Presto

  9. 9

    Extract keys and values from array with wildcard keys

  10. 10

    Extract list of duplicate values and locations from array

  11. 11

    Extract values from JSON array using XPath?

  12. 12

    How to Extract Values from php array

  13. 13

    Extract array of key-values from JSON

  14. 14

    How to extract unique values from an array EXCEPT specified values?

  15. 15

    Extract array of specific member values from array of class instances in Ruby

  16. 16

    Python, looking to extract values from a masked array, then rebuild an array

  17. 17

    Unable to extract values from object array, to which javascript array was deserialized

  18. 18

    Extract Object Values into an Array

  19. 19

    How to extract multiple values from a string to call an array?

  20. 20

    PHP: How to extract() values from an associative array with hyphens/dashes in their keys?

  21. 21

    SQL - Extract values from key value pairs to array

  22. 22

    Extract certain values from XML response to create custom JSON array

  23. 23

    extract all values of a specific field from a struct array

  24. 24

    PHP extract values from an array and pass them to a function

  25. 25

    extract values from multi dimensional array in php (mandrill)

  26. 26

    Extract values from a list using an array with boolean expressions

  27. 27

    Extract 14-bit values from an array of bytes in C

  28. 28

    How to extract number values from an array and then combine it with another string?

  29. 29

    How to use json array to extract different values from JSON document?

HotTag

Archive