Search code examples
javascriptarraysarray-push

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

Solution

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