我有一个组合数组的函数,如果数组有一些值。 这是我的阵列:
var emailAddr = [{
customerID : "C20061102105257_678",
emails : ["fandi@gmail.com", "fanjavaid@gmail.com"]
}];
var dataEmail2 = {
customerID : "C20061102105257_678",
emails : ["weisk@gmail.com"]
};
var dataEmail3 = {
customerID : "C20061102105257_600",
emails : ["eko@gmail.com"]
};
var dataEmail4 = {
customerID : "C20061102105257_678",
emails : ["1@gmail.com", "156@gmail.com"]
};
这是我的功能:
function addEmail(emailAddresses) {
if(emailAddr.length == 0) {
emailAddr.push(emailAddresses);
} else {
// Checking
for (var i = 0; i < emailAddr.length; i++) {
if (emailAddr[i].customerID == emailAddresses.customerID) {
for (var a = 0; a < emailAddresses.emails.length; a++) {
emailAddr[i]['emails'].push(emailAddresses.emails[a]);
}
} else {
emailAddr.push(emailAddresses);
}
}
}
} // End function
addEmail(dataEmail2);
addEmail(dataEmail3);
addEmail(dataEmail4);
console.log(emailAddr);
但是当我执行以下语句中的else语句时,我的浏览器崩溃了:
emailAddr.push(emailAddresses);
如何处理? 谢谢。
答案 0 :(得分:3)
这可能有用。我想你正试图拥有一系列独特的customerID,其中包含所有电子邮件。
function addEmail(emailAddresses) {
if(emailAddr.length == 0) {
emailAddr.push(emailAddresses);
} else {
// Checking
var tempCheck = true;
for (var i = 0; i < emailAddr.length; i++) {
if (emailAddr[i].customerID == emailAddresses.customerID) {
for (var a = 0; a < emailAddresses.emails.length; a++) {
emailAddr[i]['emails'].push(emailAddresses.emails[a]);
}
tempCheck = false;
}
}
if(tempCheck)
{
emailAddr.push(emailAddresses);
}
}
} // End function
&#13;
答案 1 :(得分:2)
这里有两个问题。
1 - 通过添加到当前正在阅读的数组,您将获得无限循环。这将导致它扩展,你将继续反复阅读这个扩展。
2 - 您实际上并没有将NEW对象推送到数组中,只需添加对现有对象的引用即可。这里会发生的是,当您添加到对象时,您还要添加到刚刚添加的对象。换句话说,你在这里有另一个无限循环
for (var a = 0; a < emailAddresses.emails.length; a++) {
emailAddr[i]['emails'].push(emailAddresses.emails[a]);
}
当你推送到你的数组时,你实际上正在推动引用,这意味着它也将无限扩展。要解决此问题,请使用.slice(0)来复制数组,如下所示。
var emailAddr = [{
customerID: "C20061102105257_678",
emails: ["fandi@gmail.com", "fanjavaid@gmail.com"]
}];
var dataEmail2 = {
customerID: "C20061102105257_678",
emails: ["weisk@gmail.com"]
};
var dataEmail3 = {
customerID: "C20061102105257_600",
emails: ["eko@gmail.com"]
};
var dataEmail4 = {
customerID: "C20061102105257_678",
emails: ["1@gmail.com", "156@gmail.com"]
};
function addEmail(emailAddresses) {
if (emailAddr.length == 0) {
emailAddr.push({ customerID: emailAddresses.customerID, emails: emailAddresses.emails.slice(0) });
} else {
// Checking
var success = false;
for (var i = 0; i < emailAddr.length; i++) {
if (emailAddr[i].customerID == emailAddresses.customerID) {
success = true;
for (var a = 0; a < emailAddresses.emails.length; a++) {
console.log("adding : " + emailAddresses.emails[a]);
emailAddr[i]['emails'].push(emailAddresses.emails[a]);
}
}
}
if (!success) {
emailAddr.push({ customerID: emailAddresses.customerID, emails: emailAddresses.emails.slice(0) });
}
}
} // End function
addEmail(dataEmail2);
addEmail(dataEmail3);
addEmail(dataEmail4);
console.log(emailAddr);
答案 2 :(得分:0)
function addEmail(emailAddresses) {
if(emailAddr.length == 0) {
emailAddr.push(emailAddresses);
} else {
// Checking
for (var i = 0; i < emailAddr.length; i++) {
if (emailAddr[i].customerID == emailAddresses.customerID) {
for (var a = 0; a < emailAddresses.emails.length; a++) {
// emailAddr[a]['emails'].push(emailAddresses.emails[a]);
//replaced i with a on above line
//replaced i with a on above line
emailAddr[a]['emails'].push(emailAddresses.emails[a]);
}
} else {
emailAddr.push(emailAddresses);
}
}
}
} // End function
addEmail(dataEmail2);
addEmail(dataEmail3);
addEmail(dataEmail4);
console.log(emailAddr);