【发布时间】:2020-03-30 20:18:09
【问题描述】:
抱歉,我不知道该如何表达我的问题,因此我很难找到任何相关的解决方案。
项目
我正在创建一个脚本,用于从 Excel 文件中清除成批的原始文本...
...将每个“细胞”收集到一个数组中,并将每个“人”推入一个新数组,同时将他们各自的信息位附加到每个人。文本本身包含名字、姓氏和城市等内容。有些人在新线路上有多个城市......
*所有信息都是随机生成的,任何现实生活中的比较纯属巧合
问题
我能够清理文本并将每一行的内容转储到一个数组中,没有任何问题,但是当我尝试将只有一个城市的“行”推送到最近的“完整人”时,问题就来了.例如:
John Doe 曾住在阿拉斯加州安克雷奇;纽约,纽约;和爱荷华州得梅因。
New York, NY 和 Des Moines, IA 各自以各自的行结束,每个城市都是数组中的唯一项目。
当我将一个城市推入新数组时,我得到:
Uncaught TypeError: Cannot read property 'city' of undefined
守则
这是我正在使用的代码。具体来说,我的问题在于mergeSort(a) 函数:
function sanitize(rawData) { // Accessed from event handler on line 70
// Split the raw data into "lines"
const splitByLine = rawData.split("\n");
// Split each line into an array by "tabs"
const splitByTab = splitByLine.map(line => {
return line.split("\t");
});
// Trim each array cell and remove any empty cells
const cleaned = splitByTab.map(function(item) {
const scrubbed = item.map(chunk => {
return chunk.trim();
});
const trimmed = scrubbed.filter(chunk => {
return chunk !== "";
});
return trimmed;
});
// Remove any empty arrays
const droppedEmpties = cleaned.filter(arrayItem => {
return arrayItem.length > 0;
});
mergeSort(droppedEmpties); // Line 32
}
// Sort cities lived in for each person
function mergeSort(a) {
console.log(a);
function Person(firstName, lastName, gender, city, birthday) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.city = [city];
this.birthday = birthday;
}
const people = [];
let toggleID = 0;
a.forEach(person => {
// If the entry is likely to be a person...
if (person.length > 1) {
// Set the ID for the last known person
toggleID = a.indexOf(person);
let newPerson = new Person(
person[0], // firstName
person[1], // lastName
person[2], // gender
person[3], // city
person[4] // birthday
);
people.push(newPerson);
console.log(people[toggleID]);
} else { // ...Else assume it's a city and push to the previous person.
people[toggleID].city.push(person[0]); // THROWS ERROR: Cannot read property of 'city' of undefined
}
});
}
// Get form submission
document.getElementById("my-form").addEventListener("submit", function(e) {
e.preventDefault();
const textarea = document.getElementById("my-textarea");
const rawData = textarea.value;
sanitize(rawData); // Line 1
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form id="my-form">
<button>Submit</button>
<textarea id="my-textarea" cols="150" rows="15">
Cynthia Hogsett Female Grand Rapids, MI 12/6/1983
Jacqueline Crosborne Female Syracuse, NY 3/19/1984
Ricky Johnson Male Tioga, ND 4/6/1972
Jimmy Jessup Male Ames, IA 10/27/1993
Williston, ND
Grand Forks, ND
Andrew Manchez Male El Paso, TX 3/1/2001
Sarah Smith Female Seattle, WA 7/19/1981
Esteban Faccin Male Gilbert, AZ 1/30/1973
Spokane, WA
Minot, ND
James Town, CO
</textarea>
</form>
<script src="main.js"></script>
</body>
</html>
我的猜测是,尽管我在 IF/ELSE 范围之外定义了 let toggleID,但我无法改变 IF statement 中的值并从 ELSE statement 中访问更新后的值。
提前感谢您的任何见解!
【问题讨论】:
标签: javascript arrays object if-statement variables