将 textarea 中的常量数组值除以选定的数字作为单独编号的新数组
Divide an constant array value in textarea with a selected number to be an indvidual numbered new array
我需要一个 javascript 函数,可用于在单击按钮后在 textarea 中划分(使用指定的定义数字)数组
<textarea id="source>
const uint8_t ArrayName [] PROGMEM = { // 10 lines
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33,
};
</textarea>
在结果文本区域中放入这个(这个样本除以 3)
<textarea id="result>
const uint8_t ArrayName_0 [] PROGMEM = {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC
};
const uint8_t ArrayName_1 [] PROGMEM = {
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
const uint8_t ArrayName_2 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22
};
const uint8_t ArrayName_3 [] PROGMEM = { // rest of line
0x33, 0x33, 0x33, 0x33, 0x33
};
</textarea>
我用的是从这里得到的这个函数,但是结果是错误的
我对将第一行的副本添加到每个结果数组的开头感到困惑
function divide() {
var lines = $('#source').val().split(/\n/);
var texts = []
for (var i = 0; i < lines.length; i++) {
if (/\S/.test(lines[i])) {
texts.push($.trim(lines[i]));
}
}
var list = texts;
var li = "";
for (var e = 0; e < list.length; e++) {
if (e % 3)
{
var lit = li + list[e] ;
li = lit.substring(0,lit.length-1);
li = li + '\n}\n';
} else
{
li = li + list[e] + '\n';
}
}
li = lit.substring(0,lit.length-1);
li = li + '\n}';
document.getElementById("result").value = li;
}
感谢在此源中提供帮助的任何人。
使用 reduce,您可以将行分组为一组 n
,然后使用一些字符串操作和数组编辑重新组合成您指定的格式
function divide(n) {
var lines = $('#source').val().trim().split(/\n/).slice(0, -1);
let ii = 0, prefix = lines.splice(0, 1)[0], result = [];
let groups = lines.reduce((b, a, i) => {
if (i % n === 0) {
b.push([a]);
ii++
} else b[ii].push(a);
return b;
}, [[]]).filter(a => a.length > 0);
groups.forEach((g,i) => {
g.unshift(prefix.replace('ArrayName', `ArrayName_${i}`))
g[g.length-1] = g[g.length-1].slice(0,-1)
g.push("}");
result.push(g.join("\n"));
})
document.getElementById("result").value = result.join("\n\n");
}
divide(3)
textarea {
height: 300px;
width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="source">
const uint8_t ArrayName [] PROGMEM = {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33,
};
</textarea>
<textarea id="result"> </textarea>
我需要一个 javascript 函数,可用于在单击按钮后在 textarea 中划分(使用指定的定义数字)数组
<textarea id="source>
const uint8_t ArrayName [] PROGMEM = { // 10 lines
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33,
};
</textarea>
在结果文本区域中放入这个(这个样本除以 3)
<textarea id="result>
const uint8_t ArrayName_0 [] PROGMEM = {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC
};
const uint8_t ArrayName_1 [] PROGMEM = {
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
const uint8_t ArrayName_2 [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22
};
const uint8_t ArrayName_3 [] PROGMEM = { // rest of line
0x33, 0x33, 0x33, 0x33, 0x33
};
</textarea>
我用的是从这里得到的这个函数,但是结果是错误的 我对将第一行的副本添加到每个结果数组的开头感到困惑
function divide() {
var lines = $('#source').val().split(/\n/);
var texts = []
for (var i = 0; i < lines.length; i++) {
if (/\S/.test(lines[i])) {
texts.push($.trim(lines[i]));
}
}
var list = texts;
var li = "";
for (var e = 0; e < list.length; e++) {
if (e % 3)
{
var lit = li + list[e] ;
li = lit.substring(0,lit.length-1);
li = li + '\n}\n';
} else
{
li = li + list[e] + '\n';
}
}
li = lit.substring(0,lit.length-1);
li = li + '\n}';
document.getElementById("result").value = li;
}
感谢在此源中提供帮助的任何人。
使用 reduce,您可以将行分组为一组 n
,然后使用一些字符串操作和数组编辑重新组合成您指定的格式
function divide(n) {
var lines = $('#source').val().trim().split(/\n/).slice(0, -1);
let ii = 0, prefix = lines.splice(0, 1)[0], result = [];
let groups = lines.reduce((b, a, i) => {
if (i % n === 0) {
b.push([a]);
ii++
} else b[ii].push(a);
return b;
}, [[]]).filter(a => a.length > 0);
groups.forEach((g,i) => {
g.unshift(prefix.replace('ArrayName', `ArrayName_${i}`))
g[g.length-1] = g[g.length-1].slice(0,-1)
g.push("}");
result.push(g.join("\n"));
})
document.getElementById("result").value = result.join("\n\n");
}
divide(3)
textarea {
height: 300px;
width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="source">
const uint8_t ArrayName [] PROGMEM = {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01,
0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33,
};
</textarea>
<textarea id="result"> </textarea>