简体   繁体   English

在同一索引值中推送多个值 JavaScript

[英]push multiple value in same index value JavaScript

i'm new to javascript我是 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM