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);
});
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);
You can try this
var abc = [];
text = 'name';
abc.push(text);
var def = [1,2,3,4,5]
abc.push(def.join(''));
let data = ['name',1,2,3,4,5] let result = [data[0],data.slice(1).join('')] console.log(result)
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.