Reputation: 53
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
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
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
Reputation: 4217
let data = ['name',1,2,3,4,5]
let result = [data[0],data.slice(1).join('')]
console.log(result)
Upvotes: 0
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