1

I have an array [1,2,3,4,5,6] and a separator '~' and I want to joint them into a new array with '~' being the separator.

I'd like the output to be [1,'~', 2,'~', 3,'~', 4,'~', 5,'~', 6].

Using Lodash I got something like:

var my_array = [1,2,3,4,5,6]
var separator = '~'
_.flatten(_.zip(my_array, new Array(my_array.length).fill(separator)))

But this feels ugly and I'm sure there is a better way.

EDIT: Even though the array above has ints I'd like this to work for any type of object.

Agam Rafaeli-Farhadian
  • 5,827
  • 3
  • 18
  • 23

7 Answers7

8

Why not in pue Javascript:

Minor Update: to account for values greater then 9

  • first join it to a string my_array.join("~")
  • then split every char .split(/\b/gi)

var my_array = [1,2,3,4,5,6,10,11]
var separator = '~'

console.info(my_array.join("~").split(/\b/gi));

Update (even if closed):

In Regard to point, other Objects. This should work, even if not a one-liner.

var myArray = [1,2,3,45,6,10,new Date()];

var newArray = myArray.reduce((p,n)=>{ 
  if(p.length){
    p.push("~");
  }
  p.push(n);
  return p;
},[]);

console.info(newArray)
winner_joiner
  • 12,173
  • 4
  • 36
  • 61
5

Nice simple forEach without a dozen temporary arrays, etc.:

var my_array = [1,2,3,4,5,6];
var result = [my_array[0]];
my_array.forEach(function(entry, index) {
  if (index > 0) {
    result.push("~", entry);
  }
});
console.log(result);

Or you can get rid of the if with a single temporary array:

var my_array = [1,2,3,4,5,6];
var result = [my_array[0]];
my_array.slice(1).forEach(function(entry, index) {
  result.push("~", entry);
});
console.log(result);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

Just throwing my hat in:

arr.map(x => [x, '~']).reduce((p, c) => p.concat(c));

This isn't very hacky, it maps every element into two elements and concats them together, it is pretty easy to generalize:

const intercalate = (arr, sep) => arr.map(x => [x, sep])
                                     .reduce((p, c) => p.concat(c))
                                     .slice(0, -1);

Or with a single reduce:

const intercalate = (arr, sep) => arr.reduce((p, c) => p.concat(c, sep)).slice(0, -1);
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
2

Here is an option using forEach -

var a = [1,2,3]
var sep = '~'
var b = []
a.forEach(function(x) { b.push(x, sep) })
b.pop() // remove the last `~`
Lix
  • 47,311
  • 12
  • 103
  • 131
1

Using _.flatMap,

var my_array = [1,2,3,4,5,6];
var separator = '~';
console.log(_.flatMap(my_array, function( v ){ return [v,separator] }).slice(0,-1));

Update:

Ensure trailing ~ is removed.

zahirdhada
  • 405
  • 4
  • 14
0

You could use Array#reduce and concat a tilde if necessary.

var array = [1, 2, 3, 4, 5, 6],
    result = array.reduce(function (r, a, i) {
        return r.concat(i ? '~' : [], a);
    }, []);
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Anosther proposal with Array#forEach

var array = [1, 2, 3, 4, 5, 6],
    result = [];

array.forEach(function (a) {
   result.push(a, '~');
});

result.length--;
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Using a traditional for loop

var nums = [1,2,3,4,5,6];
var dash = '~';

var res = [];
for (var i=0; i<nums.length; i++){
  res.push(nums[i]);
  res.push(dash);
}
res.pop();

console.log(res);
Idan
  • 5,405
  • 7
  • 35
  • 52