paresh shiyal
paresh shiyal

Reputation: 53

push multiple value in same index value JavaScript

i'm new to javascript

Problem

 ['name',1,2,3,4,5]

i need like :-

['name','12345']

code :-

var abc = [];
text = 'name';
abc.push(text);
var def = [1,2,3,4,5]
$.each(def, function(index, item) {
abc.push(item);
});

Upvotes: 0

Views: 895

Answers (4)

Hao Wu
Hao Wu

Reputation: 20669

You can use destruction assignment(...):

const array = ['name', 1, 2, 3, 4, 5];
const [key, ...value] = array;
const result = [key, value.join('')];

console.log(result);

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386560

You could join from the end until you got the wanted length of the array.

const array = ['name', 1, 2, 3, 4, 5];

while (array.length > 2) array.push(array.splice(array.length - 2, 2).join(''));

console.log(array);

Upvotes: 0

Alan Omar
Alan Omar

Reputation: 4217

let data = ['name',1,2,3,4,5]

let result = [data[0],data.slice(1).join('')]

console.log(result)

Upvotes: 0

Vishal Bartakke
Vishal Bartakke

Reputation: 294

You can try this

var abc = [];
text = 'name';
abc.push(text);
var def = [1,2,3,4,5]
abc.push(def.join(''));

Upvotes: 0

Related Questions