rohit jaiswal
rohit jaiswal

Reputation: 307

Merging of array in javascript or jquery

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.

Upvotes: 4

Views: 106

Answers (10)

Philip Bijker
Philip Bijker

Reputation: 5115

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

Upvotes: 1

Eduard
Eduard

Reputation: 167

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

Upvotes: 2

vengatesh rkv
vengatesh rkv

Reputation: 327

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

Upvotes: 1

Roland Szabo
Roland Szabo

Reputation: 153

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

Upvotes: 2

Joseph Shih
Joseph Shih

Reputation: 1294

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

Upvotes: 1

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

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

Upvotes: 1

Varun
Varun

Reputation: 1946

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.

Upvotes: 1

Junaid Ahmed
Junaid Ahmed

Reputation: 695

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/

Upvotes: 1

Tushar
Tushar

Reputation: 87203

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
}

Upvotes: 3

bjaksic
bjaksic

Reputation: 3313

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);

Upvotes: 1

Related Questions