admin管理员组

文章数量:1391925

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.

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.

Share Improve this question edited Jun 19, 2015 at 6:14 shree.pat18 21.8k3 gold badges44 silver badges63 bronze badges asked Jun 19, 2015 at 6:10 rohit jaiswalrohit jaiswal 3072 silver badges13 bronze badges
Add a ment  | 

10 Answers 10

Reset to default 3

Check if the length of both arrays.

See ments 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 final=[];

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

console.log(final);
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/7evx02zf/6/

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.

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

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

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

本文标签: Merging of array in javascript or jqueryStack Overflow