简体   繁体   中英

Combining two arrays with different number of elements

Let's assume we have this two arrays:

x = [1, 2, 3];
y = ['a', 'b'];

What would be the best way to combine them and get the following result:

newArray = ['1a', '1b', '2a', '2b', '3a', '3b'];

Here is one way of doing that:

x.reduce(function(arr, x) {
  return arr.concat(y.map(function(y) {
    return x + y;
  }));
}, []);
//=> ["1a", "1b", "2a", "2b", "3a", "3b"]

Try this:

 var x = [1, 2, 3]; var y = ['a', 'b']; var output = []; for (var i = 0; i < x.length; i++) { for (var j = 0; j < y.length; j++) { output.push(x[i]+y[j]); } } document.getElementById('output').innerHTML = JSON.stringify(output); 
 <div id="output"></div> 

Try this..

var x = [1, 2, 3];
var y = ['a', 'b'];
var newarr = [];
for(var i=0;i<x.length;i++){
 for(var j=0;j<y.length;j++){
     newarr.push(x[i]+y[j]);
 }
}
//alert(newarr);

DEMO

You could simply create a array to be returned and do a simple loop for the array that contains numbers. Inside of that loop, you create another loop for the array of combinations to the numbers ( var b=0,e=comb.length;e>b;b++ ). Using the i from the first loop ( for(var i=0,l=array.length;l>i;i++) ) you push the array at it ( a[i] ) with the array of combinations at the position b ( c[b] ) (inside of the loop that's inside of the first loop) to the new array. Finally, return the new array.

function CombineExample(a,c){
    var New=[];
    for(var i=0,l=a.length;l>i;i++){
        for(var b=0,e=c.length;e>b;b++){
             New.push(a[i]+c[b])
        }
    }
    return New
}

Clean! And do this to use:

CombineExample([1,2,3],['a','b'])
/* returns ["1a", "1b", "2a", "2b", "3a", "3b"] */

If arrow functions are supported you obtain the desired result like this:

[].concat.apply([],
    x.map(x => y.map(y => x+y))
);

If not, you have to write it like this

[].concat.apply([],
    x.map(function(x) { return y.map(function(y) {return x+y })})
);

Explanation:

The middle line yields the following result:

[ ["1a", "1b"], ["2a", "2b"], ["3a", "3b"] ]

Then the Array.prototype.concat method is used to concatenate the inner arrays.

Use nested loops to iterate all elements of the participating arrays. Populate new array elements inside the inner loop:

var x = [1, 2, 3];
var y = ['a', 'b'];

var newArray = [];
x.forEach(function(xItem) {
  y.forEach(function(yItem) {
    newArray.push(xItem.toString().concat(yItem));
  });
});

console.log(newArray);

The simplest approach:

var x = ["a", "b", "c"];
var y = [1, 2, 3];
var newArray = [];
var i = 0;
for (;i < x.length;++i) {
  var j = 0;
  for (;j < y.length;++j) {
    newArray.push(x[i] + y[j]);
  }
}
;

Please do note that if both arrays are numeric, this will actually add the numbers, not concatenate. You'd need to do some string conversion.

var x = [1, 2, 3];
var y = ['a', 'b'];
var z = [];
for(var i=0;i<x.length;i++){
    for(var j=0;j<y.length;j++){
        z.push(x[i]+y[j]);
    }
}

Are you seriously asking for that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM