gpt4 book ai didi

javascript - 如何减少javascript中的多个foreach?

转载 作者:行者123 更新时间:2023-11-30 21:09:53 25 4
gpt4 key购买 nike

如何使 forEach 部分更干净、更美观?

我将制作更多call_Fu​​ntions()

我不想一直打字:

sentences.all.forEach((s, i) => {});

只是为了记住哪些句子已经被删除了。


句子对象:

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."
]
}
}

最佳答案

保留并添加"!!!End!!!"

sentences = {
all: [
"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 ok forever.",
"!!!End!!!"
],

push_into_here: {
push_first: [],
push_second: [],
push_third: []
},
}

添加这些:

not_found = []
new_source = ["!!!End!!!"]

添加一个减少功能:(取消注释 console.log 以查看减少进度)

function Reduce_Source() {
x = not_found.slice(-1)
y = new_source[new_source.length - 1]
if (x == y) {
//console.log(new_source)
new_source = not_found
not_found = []
}
}

添加一个过滤函数:

function filterMatches(s, regexp, move_to) {
return s.filter(function(str) {
i = regexp.test(str)
if (i) {
move_to.push(str)
}
else {
not_found.push(str)
Reduce_Source()
}
return i
});
}

按照您要搜索和删除的顺序添加这些:

filterMatches(sentences.all, /First/, sentences.push_into_here.push_first)
filterMatches(new_source, /Second/, sentences.push_into_here.push_second)
filterMatches(new_source, /Last/, sentences.push_into_here.push_third)

工作演示

sentences = {
all: [
"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 ok forever.",
"!!!End!!!"
],

push_into_here: {
push_first: [],
push_second: [],
push_third: []
},
}

not_found = []

new_source = ["!!!End!!!"]

function Reduce_Source() {
x = not_found.slice(-1)
y = new_source[new_source.length - 1]
if (x == y) {
//console.log(new_source)
new_source = not_found
not_found = []
}
}

function filterMatches(s, regexp, move_to) {
return s.filter(function(str) {
i = regexp.test(str)
if (i) {
move_to.push(str)
} else {
not_found.push(str)
Reduce_Source()
}
return i
});
}

filterMatches(sentences.all, /First/, sentences.push_into_here.push_first)
filterMatches(new_source, /Second/, sentences.push_into_here.push_second)
filterMatches(new_source, /Last/, sentences.push_into_here.push_third)
document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
document.write("<pre>" + JSON.stringify(not_found, null, 2) + "</pre>");
document.write("<pre>" + JSON.stringify(new_source, null, 2) + "</pre>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="me"></div>
<div class="me2"></div>
<div class="me3"></div>

关于javascript - 如何减少javascript中的多个foreach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com