说我有一个由一位整数组成的数组
[1,0,2,3,6,7,9,8]
我将如何将它们转换为2位整数的数组?
像这样:
[10, 23, 67, 98]
答案 0 :(得分:1)
您可以缩小数组并将第n th 值用作索引。
此方法适用于每个分组数字计数。
library(stringr)
str_detect(s, "^[:upper:]+$")
const
group = (array, digits) => array.reduce((r, v, i) => {
var j = Math.floor(i / digits);
r[j] = (r[j] || 0) * 10 + v;
return r;
}, [])
console.log(group([1, 0, 2, 3, 6, 7, 9, 8], 2));
console.log(group([1, 0, 2, 3, 6, 7, 9, 8, 9], 3));
console.log(group([1, 0, 2, 3, 6, 7, 9, 8], 4));
答案 1 :(得分:0)
您可以使用Array.from()
创建一个具有原始array.length /块大小(在您的情况下为2)的长度的新数组。用Array.slice()
提取每个块,加入并转换回数字:
const chunkAndJoin = (chunkSize, arr) =>
Array.from({ length: Math.ceil(arr.length / chunkSize) }, (_, i) =>
Number(arr.slice(i * chunkSize, (i + 1) * chunkSize).join(''))
)
const arr = [1,0,2,3,6,7,9,8]
const result = chunkAndJoin(2, arr)
console.log(result)
答案 2 :(得分:0)
您可以通过使用迭代索引将Array.prototype.reduce()
和concat数字用作字符串:
const initial = [1,0,2,3,6,7,9,8];
const nextArr = initial.reduce((acc, value, index) => {
if (index % 2 !== 0) return acc;
acc.push(Number(String(value) + String(initial[index + 1])));
return acc;
}, []);
console.log(nextArr);
答案 3 :(得分:0)
您可以使用.reduce()
,方法是检查累积数组中的最后一项是否小于10(应追加该项),否则,可以将其添加到数组中。
请参见以下示例:
const arr = [1,0,2,3,6,7,9,8];
const res = arr.reduce((acc, n) => {
let last = acc.length-1;
if(acc[last] < 10) {
acc[last] = acc[last]*10 + n;
} else {
acc.push(n);
}
return acc;
}, []);
console.log(res);
答案 4 :(得分:0)
我不知道javascript:D,但是类似这样。
Exception in thread "main" java.lang.Error:
java.io.FileNotFoundException: null\lib\tzdb.dat (The system cannot find the path specified)
at sun.util.calendar.ZoneInfoFile$1.run(ZoneInfoFile.java:261)
at java.security.AccessController.doPrivileged(Native Method)
at sun.util.calendar.ZoneInfoFile.<clinit>(ZoneInfoFile.java:251)
at sun.util.calendar.ZoneInfo.getTimeZone(ZoneInfo.java:589)
at java.util.TimeZone.getTimeZone(TimeZone.java:560)
at java.util.TimeZone.setDefaultZone(TimeZone.java:666)
at java.util.TimeZone.getDefaultRef(TimeZone.java:636)
at java.util.TimeZone.getDefault(TimeZone.java:625)
at org.sqlite.date.FormatCache.getInstance(FormatCache.java:74)
at org.sqlite.date.FastDateFormat.getInstance(FastDateFormat.java:129)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:70)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:25)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at repository.JdbcUtils.getNewConnection(JdbcUtils.java:39)
at repository.JdbcUtils.getConnection(JdbcUtils.java:52)
at repository.RepositoryLog.findOne(RepositoryLog.java:28)
at Main.main(Main.java:28)
Caused by: java.io.FileNotFoundException: null\lib\tzdb.dat (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at sun.util.calendar.ZoneInfoFile$1.run(ZoneInfoFile.java:255)
... 21 more
P.S。,如果您的数组长度%2!= 0,则此代码将跳过最后一个元素))
答案 5 :(得分:0)
您可以使用法线进行循环并将i
乘以2
并乘以10
let array = [1, 0, 2, 3, 6, 7, 9, 8]
let finalArray = [];
for (let i = 0; i < array.length; i += 2) {
finalArray.push(array[i]*10 + (array[i + 1]))
}
console.log(finalArray)