2017-09-17 36 views
1

如何使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." 
     ] 
    } 
} 
+0

如果复制确实困扰着你,你总是可以只包住'句子.all.forEach(...在一个函数中。 – Carcigenicate

+0

请添加所有的通缉令。 –

+0

我不太了解你想要达到的目标。可以更多地解释它。 –

回答

1

保持sentences并添加"!!!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>

0

你可以使用一个循环和过滤的价值观和复制句子想要的阵列。

对于每个找到的字符串,都会调用一个指定的函数。

var 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 forever."], push_into_here: { push_first: [], push_second: [], push_third: [] } }, 
 
    target = { 
 
     First: { key: 'push_first', fn: function (s) { console.log(1, s); } }, 
 
     Second: { key: 'push_second', fn: function (s) { console.log(2, s); } }, 
 
     Last: { key: 'push_third', fn: function (s) { console.log(3, s); } } 
 
    }, 
 
    regex = new RegExp(Object.keys(target).join('|'), 'g'); 
 

 
sentences.all = sentences.all.filter(function (s) { 
 
    var temp = s.match(regex); 
 

 
    if (temp) { 
 
     target[temp[0]].fn(s); 
 
     sentences.push_into_here[target[temp[0]].key].push(s); 
 
     return false; 
 
    } 
 
    return true; 
 
}); 
 

 
console.log(sentences);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

这是很好的代码......但我必须一个接一个做......我将优先考虑匹配功能。所以句子将与多种功能相匹配......这是优先考虑每个功能的优先顺序。每个功能将有不同数量的段落。 –

+0

看到...我试过的东西 –

+0

有些功能会有更多的功能,然后是其他功能。我将有10种类型的功能。 –

0

1)分散你的函数的参数和使用过滤器:

const call_Funtion1 = (looking_for, move_to) => { 
    const test = new RegExp(looking_for, 'i'); 
    return s => !(test.test(s)?move_to.push(s):0); 
    }; 

2)链中过滤来电:

sentences.all = 
    sentences.all.filter(
    call_Funtion1(/First/g, push_into_here.push_first) 
).filter(
    call_Funtion2(/Second/g, push_into_here.push_second, para5, para6) 
).filter(
    call_Funtion1(/Last/g, push_into_here.push_third) 
); 
0

由于delete操作不总是产生最有效的代码,你co (

  • 限制函数仅返回验证结果,不包括移动操作(删除,推送)。
  • 首先确定每个值应通过建立与目的地名称的数组(['push_first', 'push_first', 'push_third', null, ...],其中null表示去的项目不应该移动
  • 然后使用filter来定义最终阵列:

代码:

// Let your functions only return the validation result 
function call_Funtion1(s, looking_for){ 
    check_for = new RegExp(looking_for, 'i'); 
    return check_for.test(s); 
} 

function call_Funtion2(s, looking_for, para5, para6){ 
    //Code is running to check if this sentence is true 
    // Do something... 
    return (/*this sentence is true*/); 
} 

// First determine where each element should go: 
const targets = sentences.all.map((s, i) => { 
    return call_Funtion1(s, /First/g) ? 'push_first' 
     : call_Funtion2(s, /Second/g, 'para5', 'para6') ? 'push_second' 
     : call_Funtion1(s, /Last/g) ? 'push_third' 
     : null; 
}); 

// Now move them using filter: 
Object.keys(push_into_here).forEach((key) => { 
    push_into_here[key] = sentences.all.filter((s, i) => targets[i] == key); 
}); 
sentences.all = sentences.all.filter((s, i) => !targets[i]);