4
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.

10 Answers 10

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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'],'~');

Comments

2

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' ]

Comments

1
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
var final=[];

// assuming they are the same length
for(var i = 0; i < first.length; i++) {
  final.push(first[i] + '~' + second[i]);
}

console.log(final);

Comments

1
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/

Comments

1
var final1=[];
var first = [ "a", "b", "c" ];
var second = [ "d", "e", "f" ];
$.each(first, function( index, value ) {
      var tmp=value+"~"+second[index];
      final1[index]=tmp;
});
 console.log(final1);

JQUERY only solution.Try this,should work.

Comments

1

Try this...

<script>

var first = ["a", "b", "c"];
var second = ["d", "e", "f"];
var third=new Array();

for(i=0;i<first.length;i++)
{

var data=first[i]+'~'+second[i];
third.push(data);

}
console.log(third);
</script>

Output:["a~d", "b~e", "c~f"]

Comments

1

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"]

Comments

1

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

Comments

1

You could try this JS only solution. It's only 1 line and works regardless of both first and second lengths:

var first = [ "a", "b", "c", "x", "y" ];
var second = [ "d", "e", "f" ];
var final = [];

first.forEach(function(v,i) {final[i] = first[i] + '~' + (second[i] || '')});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.