如何使forEach部分更清洁和更好?如何减少JavaScript中的多个foreach?
我将作出更多call_Funtions()
我不希望有继续键入:
sentences.all.forEach((s, i) => {});
只要记住这句话已经被删除。
sentence
对象:
sentences = {
all: [
//100s of sentences will be placed here.
"First condition was met, so push and delete this forever.",
"Second condition was met, so push and delete this forever.",
"Second condition was met, so push and delete this forever.",
"First condition was met, so push and delete this forever.",
"First condition was met, so push and delete this forever.",
"Last condition was met, so push and delete this forever.",
"No conditions were met, so this sentence stays here.",
"Last condition was met, so push and delete this forever.",
"No conditions were met, so this sentence stays here.",
"No conditions were met, so this sentence stays here.",
"Last condition was met, so push and delete this forever.",
"Second condition was met, so push and delete this forever."
],
push_into_here: {
push_first: [],
push_second:[],
push_third: []
}
}
第一功能:是推动然后删除该句子是真的
function call_Funtion1(s, i, looking_for, move_to){
check_for = new RegExp(looking_for, 'i');
found_them = check_for.test(s);
if (found_them) {
move_to.push(s)
delete sentences.all[i]
}
}
第二功能:是推动然后删除这是真的
function call_Funtion2(s, i, looking_for, move_to, para5, para6){
//Code is running to check if this sentence is true
if (/*this sentences is true*/) {
//Do something
//Do something else
move_to.push(s)
delete sentences.all[i]
}
}
forEach
了一句:剩下的句子内sentences.all
//More call_Functions will be added
//Possibly 10 or more
//This will run first, deleting all matching sentences after being pushed
sentences.all.forEach((s, i) => {
call_Funtion1(s, i, /First/g, push_into_here.push_first)
});
//This will run second, deleting all matching sentences after being pushed
sentences.all.forEach((s, i) => {
call_Funtion2(s, i, /Second/g, push_into_here.push_second, para5, para6)
});
//This will run last, deleting all matching sentences after being pushed
sentences.all.forEach((s, i) => {
call_Funtion1(s, i, /Last/g, push_into_here.push_third)
});
我已经试过:
个//I tried doing this
//But it doesn't remember which ones were previously deleted
sentences.all.forEach((s, i) => {
call_Funtion1(s, i, /First/g, push_into_here.push_first)
call_Funtion2(s, i, /Second/g, push_into_here.push_second, para5, para6)
call_Funtion1(s, i, /Last/g, push_into_here.push_third)
});
结果应该是:
sentences = {
all: [
//100s of sentences will be placed here.
"No conditions were met, so this sentence stays here.",
"No conditions were met, so this sentence stays here.",
"No conditions were met, so this sentence stays here."
],
push_into_here: {
push_first: [
"First condition was met, so push and delete this forever.",
"First condition was met, so push and delete this forever.",
"First condition was met, so push and delete this forever."
],
push_second:[
"Second condition was met, so push and delete this forever.",
"Second condition was met, so push and delete this forever.",
"Second condition was met, so push and delete this forever."
],
push_third: [
"Last condition was met, so push and delete this forever.",
"Last condition was met, so push and delete this forever.",
"Last condition was met, so push and delete this forever."
]
}
}
如果复制确实困扰着你,你总是可以只包住'句子.all.forEach(...在一个函数中。 – Carcigenicate
请添加所有的通缉令。 –
我不太了解你想要达到的目标。可以更多地解释它。 –