gpt4 book ai didi

javascript - 将数组从一行数组转换为多行数组

转载 作者:行者123 更新时间:2023-11-30 12:04:00 29 4
gpt4 key购买 nike

我有一个数组返回这样的值:

0: 1,2,3,4

我需要它返回这样的数组值:

0: 1
1: 2
2: 3
3: 4

如果我用 javascript 来做,我该怎么做呢?

最佳答案

用一个循环,例如。

var object = { '0': [1, 2, 3, 4] },       // the object
result = function (o) { // the iife for the result
var r = {}; // the temporary variable
o['0'].forEach(function (a, i) { // the loop over the property zero
r[i] = a; // the assignment to the object with
}); // the wanted key
return r; // the return of the temporary object
}(object); // the start with the object

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

关于javascript - 将数组从一行数组转换为多行数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794730/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com