0

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

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this

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

Comments

0

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

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

console.log(result)

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.