拆分数组的一个元素并请求“长度”返回错误[关闭]
2021-07-16
119
我试图拆分数组中的一个元素(它是一个数组),然后访问该数组的长度。
示例输入可以是:“他走路。她走路。”。
我的代码(删除了不相关的部分)是:
function image(input, str) {
let nos = str.split(".");
let sub = [];
let words = [];
let ret = [];
let isNoun = function (word, zzVal, zVal) {
let letters = word;
letters = letters.replace(/[^A-Za-z]/gi, "");
//if (input[z - 1])
letters = letters.split("");
return letters.join("");
}
for ( let zz = 0; zz < nos.length; zz++ ) {
ret[zz] = [];
if ( nos[ zz ] !== "" ) {
sub.push( nos[ zz ] );
}
for ( let b = 0; b < sub.length; b++ ) {
words.push((sub[ b ].split(" ").filter(e => e !== "")));
}
alert( words );
for (let z = 0; z < words[ zz ].length; z++) {
let n = input[(zz * words[ zz ].length) + z].length;
let x = 1;
let y = 1;
let dmn = [x, y];
while ((x * y) < n) {
if (n % 2 == 0) {
x++;
} else {
x += 2;
}
y = n / x;
if (y == 1 || !Number.isInteger(x)) {
x = 2;
y = 2;
while ((x * y) < (n + 1)) {
y++;
x = (n + 1) / y;
}
}
dmn = [x, y];
}
let a = 0;
ret[zz][z] = [];
for (let b = 0; b < y; b++) {
ret[zz][z][b] = [];
for (let c = 0; c < x; c++) {
ret[zz][z][b][c] = input[z][a];
a++;
}
}
document.getElementById("output").innerHTML += "<div id='" + zz + "' class='sentence'><div id='" + zz + "-bottom" + "'></div><div id='" + zz + "-line'></div><div id='" + zz + "-top'></div></div>";
alert(isNoun(str.split(" ")[z], zz, z));
document.getElementById("output").innerHTML += "<table id='" + zz + "-" + z + "' class='word'></table>";
for (let a = 0; a < y; a++) {
document.getElementById(zz + "-" + z).innerHTML += "<tr id='" + zz + "-" + z + "-" + a + "'></tr>";
for (let b = 0; b < x; b++) {
if (Number(ret[zz][z][a][b])) {
document.getElementById(zz + "-" + z + "-" + a).innerHTML += "<td id='" + zz + "-" + z + "-" + a + "-" + b + "'></td>";
} else {
document.getElementById(zz + "-" + z + "-" + a).innerHTML += "<td id='" + zz + "-" + z + "-" + a + "-" + b + "' style='background: #eee;'></td>";
}
}
}
}
}
}
这给出了:
Uncaught TypeError: Cannot read property 'length' of undefined
我想要的输出是一个数组,如果
image(null, "He walks. She walks.")
则为:
[["He", "walks"], ["She", "walks"]]
。
2个回答
function image(input, str) {
// Remove dots
let nos = str.split(".")
// Remove empty space after dots
for (let i=0;i<nos.length;i++){
if(nos[i][0]===" "){
nos[i]=nos[i].substring(1)
}
}
// Remove empty array element
nos=nos.filter(item=>item!=="")
// Console log array elements length
for (let i=0;i<nos.length;i++){
console.log(nos[i],nos[i].length)
}
}
image(null, "He walks. She walks.");
Yhunie-the-cat
2021-07-17
由于您的最后一个问题不清楚,并且您正在更新所需的输出,因此您可以尝试以下解决方案
function image(input, str) {
let nos = str.split(".");
var newArray = [] //create newArray from (.) symbol
for (let i = 0; i < nos.length; i++){
if (nos[i] != ""){
newArray.push([nos[i]])
}
}
console.log(newArray) //[ [ 'He walks' ], [ ' She walks' ] ]
//you can see the (space) behind S in She walks
//lets remove it
var secondArray = []
for (let i = 0; i < newArray.length; i++){
// this metode will help you to push array that aren`t has space
secondArray.push((newArray[i][0].split(" ").filter(e => e !== "")))
}
console.log(secondArray) // [ [ 'He', 'walks' ], [ 'She', 'walks' ] ]
}
image(null, "He walks. She walks.")
Ded the Super Duck
2021-07-17