var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
My output should be
var final=["a~d","b~e","c~f"];
where '~' is delimiter.
Check if the length of both arrays.
See comments inline in the code:
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
var len = first.length > second.length ? first.length : second.length;
// Get the length of the array having max elements
var separator = '~';
var final = [];
for(var i = 0; i < len; i++) {
// If no element is present, use empty string
first[i] = first[i] ? first[i] : '';
second[i] = second[i] ? second[i] : '';
final.push(first[i] + separator + second[i]);
// Add the new element in the new array
}
Here is a function for this... you can specify the behavior, if the arrays are not the same length:
function merge(arr1,arr2,delimiter){
var i,len;
var delim = delimiter.toString();
var res = [];
if(arr1.length !== arr2.length){
//TODO: if arrays have different length
}else{
len = arr1.length;
for(i=0; i< len; i++){
res[i] = arr1[i] + delim + arr2[i];
}
}
return res;
}
merge(['a','b','c'],['d','e','f'],'~');
This is exactly what Haskell's zipWith function does. It takes a function (which takes two arguments and does something with them to return only one value) and two arrays, looping through the arrays and passing it's values into the function.
Here is a non-recursive example:
var zipWith = function(zippingFunction, arr1, arr2){
var length = arr1.length < arr2.length ? arr1.length : arr2.length;
var retArray = [];
for (i = 0; i< length; i++){
retArray.push(zippingFunction(arr1[i], arr2[i]));
}
return retArray;
};
console.log(zipWith(function(a, b){return a + b}, [1,2,3], [4,5,6]));
console.log(zipWith(function(a, b){return a + "~" + b}, ["1","2","3"], ["4","5","6"]));
Which returns:
[ 5, 7, 9 ]
[ '1~4', '2~5', '3~6' ]
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
var count = -1;
var arr=new Array();
first.forEach(function(entry) {
count++;
arr[count] = entry + "~" + second[count];
});
alert(arr);
Use Like This You Get Your Desired Result
Demo is Here http://jsfiddle.net/7evx02zf/6/
A JavaScript only solution. Try this. This solution assumes that both arrays are equal in length.
//your initial array
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
//set up final array
var final = [];
//loop through
for (var ii = 0, nn = first.length; ii < nn; ii++)
{
//add to final array each item of first and secon
final.push(first[ii]+"~"+second[ii]);
}
//print to console
console.log(final);
Output:
["a~d", "b~e", "c~f"]
If you're not sure if length are the same, this will go up to the shortest
//your initial array
var first = [ "a", "b", "c", "g" ];
var second = [ "d", "e", "f" ];
//set up final array
var final = [];
//loop through until the length of shortest
for (var ii = 0, nn = (first.length < second.length ? first.length : second.length); ii < nn; ii++)
{
//add to final array each item of first and secon
final.push(first[ii]+"~"+second[ii]);
}
//print to console
console.log(final);
Output:
["a~d", "b~e", "c~f"]
Try this code:
var final=[];
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
for(var i = 0; i < first.length; i++) {
final.push(first[i] + '~' + second[i]);
}
console.log(final.toString());
Example:
Click Here for Demo