6 回答
TA贡献1871条经验 获得超8个赞
您可以使用 split 函数两次来创建嵌套数组:
let str = "1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|";
let arr = str.split("|").map(r => r.split("*"));
console.log(arr);
TA贡献1869条经验 获得超4个赞
也许有点过时,但同样有效
var a ="1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00";
var b = a.split("|");
var c = [];
for (i = 0; i < b.length; i++) {
c[i] = b[i].split("*");
}
TA贡献1712条经验 获得超3个赞
您可以使用多个 delemiter 进行拆分。
const str = "1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|"
const splitStr = str.split(/\*|\|/);
console.log(splitStr);
TA贡献1921条经验 获得超9个赞
如果您想一次过一遍,这应该创建一个表示数组/列的嵌套数组结构。它也很粗糙,可能会稍微简化一下。
const fullStr = "1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|";
const arr = [[]];
for(let i = 0, str = ''; i < fullStr.length; i++){
if(fullStr[i] == '*') {
arr[arr.length - 1].push(str);
str = '';
continue;
}
if(fullStr[i] == '|') {
arr[arr.length -1].push(str);
// you could wrap this push statement in an if to determine if you are at the end of the string.
arr.push([]);
str = '';
continue;
}
str += fullStr[i];
}
console.log(arr);
TA贡献1818条经验 获得超7个赞
这里已经有很多好的答案——这将返回一个具有行/列属性的单元格对象数组:
let mystring = "1*264.75|2*4936.00|3*8230.76|4*8329.75|5*3106.25|6*3442.00|7*5122.50|10*77.00|11*7581.00|12*7573.25|13*3509.00|21*5246.50|24*4181.00|25*4961.25|52*34.00|";
let cells = mystring.split("|");
let results = [];
for (let i = 0; i < cells.length; i++) {
let cell = cells[i];
let cellData = cell.split("*");
results.push({
row: cellData[0],
col: cellData[1]
})
}
console.log(results);
TA贡献1862条经验 获得超7个赞
const input = "1264.75|24936.00|38230.76|48329.75|53106.25|63442.00|75122.50|1077.00|117581.00|127573.25|133509.00|215246.50|244181.00|254961.25|52*34.00|"
// The filter is for removing empty rows
input.split("|").filter(val=>!!val).map(val => val.split("*").map(res=>parseFloat(res)))
结果会像
[[1264.75],[24936],[38230.76],[48329.75],[53106.25],[63442],[75122.5],[1077],[117581],[127573.25],[133509],[215246.5],[244181],[254961.25],[52,34]]
添加回答
举报