admin管理员组文章数量:1026989
I have arrays like [a]
, [a,b]
, [a,b,c]
and so on.
How can I convert them into [a]
, [a][b]
, [a][b][c]
and so on?
Example:
var arr = [1,2,3,4];
arr = do(arr); // arr = arr[1][2][3][4]
I have arrays like [a]
, [a,b]
, [a,b,c]
and so on.
How can I convert them into [a]
, [a][b]
, [a][b][c]
and so on?
Example:
var arr = [1,2,3,4];
arr = do(arr); // arr = arr[1][2][3][4]
Share
Improve this question
asked Jul 20, 2016 at 11:09
VahidVahid
3,4424 gold badges36 silver badges71 bronze badges
2
-
The expression
[a][b][c]
means, "Create anArray
:[a]
. Index that array with the valueb
. Index that value with the valuec
." Is that what you want to acplish? – A. Vidor Commented Jul 20, 2016 at 11:17 - @this-vidor I mean 3D, 4D, ... array if you mean that. – Vahid Commented Jul 20, 2016 at 11:27
4 Answers
Reset to default 9You could map it with Array#map
.That returns an array with the processed values.
ES6
console.log([1, 2, 3, 4].map(a => [a]));
ES5
console.log([1, 2, 3, 4].map(function (a) {
return [a];
}));
While the question is a bit unclear, and I think the OP needs possibly a string in the wanted form, then this would do it.
console.log([1, 2, 3, 4].reduce(function (r, a) {
return r + '[' + a + ']';
}, 'arr'));
Functional:
use .map
like this
[1,2,3,4].map(i => [i])
Iterative:
var list = [1, 2, 3, 4], result = [];
for (var i=0; i<list.length; i++) {
result.push([list[i]]);
}
If I understand you correctly, you are converting single dimension array to multi dimensional array. To do so,
var inputArray = [1,2,3,4];
var outputArray = [];
for(var i=0;i<inputArray.length;i++)
{
outputArray.push([inputArray[i]])
}
function map(arr){
var aux = [];
for(var i=0; i<arr.length;++i){
var aux2 = [];
aux2.push(arr[i]);
aux.push(aux2);
}
return aux;
}
I have arrays like [a]
, [a,b]
, [a,b,c]
and so on.
How can I convert them into [a]
, [a][b]
, [a][b][c]
and so on?
Example:
var arr = [1,2,3,4];
arr = do(arr); // arr = arr[1][2][3][4]
I have arrays like [a]
, [a,b]
, [a,b,c]
and so on.
How can I convert them into [a]
, [a][b]
, [a][b][c]
and so on?
Example:
var arr = [1,2,3,4];
arr = do(arr); // arr = arr[1][2][3][4]
Share
Improve this question
asked Jul 20, 2016 at 11:09
VahidVahid
3,4424 gold badges36 silver badges71 bronze badges
2
-
The expression
[a][b][c]
means, "Create anArray
:[a]
. Index that array with the valueb
. Index that value with the valuec
." Is that what you want to acplish? – A. Vidor Commented Jul 20, 2016 at 11:17 - @this-vidor I mean 3D, 4D, ... array if you mean that. – Vahid Commented Jul 20, 2016 at 11:27
4 Answers
Reset to default 9You could map it with Array#map
.That returns an array with the processed values.
ES6
console.log([1, 2, 3, 4].map(a => [a]));
ES5
console.log([1, 2, 3, 4].map(function (a) {
return [a];
}));
While the question is a bit unclear, and I think the OP needs possibly a string in the wanted form, then this would do it.
console.log([1, 2, 3, 4].reduce(function (r, a) {
return r + '[' + a + ']';
}, 'arr'));
Functional:
use .map
like this
[1,2,3,4].map(i => [i])
Iterative:
var list = [1, 2, 3, 4], result = [];
for (var i=0; i<list.length; i++) {
result.push([list[i]]);
}
If I understand you correctly, you are converting single dimension array to multi dimensional array. To do so,
var inputArray = [1,2,3,4];
var outputArray = [];
for(var i=0;i<inputArray.length;i++)
{
outputArray.push([inputArray[i]])
}
function map(arr){
var aux = [];
for(var i=0; i<arr.length;++i){
var aux2 = [];
aux2.push(arr[i]);
aux.push(aux2);
}
return aux;
}
本文标签: arraysJavaScript Convert abc into abcStack Overflow
版权声明:本文标题:arrays - JavaScript: Convert [a,b,c] into [a][b][c] - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1742183730a1919335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论