nha
nha

Reputation: 18005

Javascript ES6 - map multiple arrays

Is there a feature in JavaScript 6 that allows to map over multiple arrays ?

Something like a zipper :

 var myFn = function (a, b) { console.log(a, b);}
  var arr1 = ['a', 'b', 'c'];
  var arr2 = [1, 2, 3];

  arr1.map(myFn, arr2); // imaginary syntax.
  // prints :
  // a 1
  // b 2
  // c 3

Upvotes: 25

Views: 65485

Answers (5)

jared soto
jared soto

Reputation: 11

Late to the party, but here's my solution.

// Custom zip
const zip = function (N, callback) {
    if (this.length === 0 || N.length === 0 || this.length !== N.length) {
        console.error(`Arrays are of different lengths or empty.`);
        return;
    }
    for (let i = 0; i < this.length; i++) {
        callback(this[i], N[i], i, this, N);
    }
};

// Custom zipMap
const zipMap = function (N, callback) {
    if (this.length === 0 || N.length === 0 || this.length !== N.length) {
        console.error(`Arrays are of different lengths or empty.`);
        return;
    }
    const results = [];
    for (let i = 0; i < this.length; i++) {
        results.push(callback(this[i], N[i], i, this, N));
    }
    return results;
};

// Basic Usage:
const numberArr = [1,2,3];
const stringArr = ['a','b','c'];

// Add zip function to array as a property.
numberArr.zip = zip;
//or
numberArr.zipMap = zipMap;

const callback = function(itemA, itemB, index, ArrayA, ArrayB){
    console.log(itemA + itemB)
}

numberArr.zip(stringArr, callback);

// prints :
// a1
// b2
// c3

You can also add the functions to Array.prototype but I don't recommend doing so, unless it's only going to be used in your code base. This post explains why it should be avoided.

//Use with caution
Object.defineProperty(Array.prototype, 'zip', {
    value: zip
});

Object.defineProperty(Array.prototype, 'zipMap', {
    value: zipMap
});

Upvotes: 0

ciaooociaooociaooo
ciaooociaooociaooo

Reputation: 11

Took reference from @Onaracs 's answer and edited it.

const arr1 = ['a', 'b', 'c']
const arr2 = [1, 2, 3]

const arr3 = arr1.reduce((acc, current, index) => {
  return [...acc, [current, arr2[index]]]
}, [])

console.log(arr3)

Upvotes: 1

Onaracs
Onaracs

Reputation: 935

You could also use reduce to get the desired outcome:

var arr1 = ['a', 'b', 'c'];
var arr2 = [1, 2, 3];

arr1.reduce((acc, current, index) => {
   console.log(current, arr2[index])
   return [...acc, current, arr2[index]]
}, [])

// a 1
// b 2
// c 3
// returns ["a", 1, "b", 2, "c", 3]

Upvotes: 8

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276446

As the other answer points out, this is commonly known as a zip. It can be implemented as:

let zipped = arr1.map((x, i) => [x, arr2[i]]);

Or as a function, basically:

let zip = (a1, a2) => a1.map((x, i) => [x, a2[i]]); 

Which would let you do:

zip(["a","b","c"], [1,2,3]); // ["a", 1], ["b", 2], ["c", 3]

Upvotes: 44

lukewestby
lukewestby

Reputation: 1207

Unfortunately, no. What you are looking for is commonly called zip or zipWith. See lodash's implementation for a reference: https://lodash.com/docs#zipWith

Upvotes: 10

Related Questions