如何清空和删除带参数的数组?
How to empty and delete an array with a parameter?
问题
- 我无法使用
s
参数清空数组
- 我不能
delete
一个带有deleteThis
参数的数组
sentences = {
all: [
//1,000s of Sentences
"This sentence meets all conditions.",
"This sentence meets all conditions.",
"This sentence failed.",
"This sentence failed.",
"This sentence meets all conditions.",
"This sentence failed."
],
success: [],
failed: [],
moveTo_then_Delete: []
}
function Divide_And_Empty(s, success, failed, deleteThis, regEx) {
s.filter(function(str) {
key = regEx.test(str)
if (key) {
success.push(str)
} else {
failed.push(str)
deleteThis.push(str)
}
});
//The code works correctly
//All the way up until I try to empty and/or delete an array with parameter
s = []
delete deleteThis
}
Divide_And_Empty(sentences.all, sentences.success, sentences.failed, sentences.moveTo_then_Delete, /meets/)
document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
在您的示例中,s
指的是您在参数中传递的数组。你只能改变这个数组。
如果你想从名为 s
的数组中删除所有元素,那么你应该使用
s.length = 0;
或
s.splice(0, s.length)
而不是 s = []
:
sentences = {
all: [
//1,000s of Sentences
"This sentence meets all conditions.",
"This sentence meets all conditions.",
"This sentence failed.",
"This sentence failed.",
"This sentence meets all conditions.",
"This sentence failed."
],
success: [],
failed: [],
}
function Divide_And_Empty(s, success, failed, regEx) {
s.filter(function(str) {
key = regEx.test(str)
if (key) {
success.push(str)
} else {
failed.push(str)
}
});
s.length = 0; // or s.splice(0, s.length);
}
Divide_And_Empty(sentences.all, sentences.success, sentences.failed, /meets/);
document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
问题
- 我无法使用
s
参数清空数组 - 我不能
delete
一个带有deleteThis
参数的数组
sentences = {
all: [
//1,000s of Sentences
"This sentence meets all conditions.",
"This sentence meets all conditions.",
"This sentence failed.",
"This sentence failed.",
"This sentence meets all conditions.",
"This sentence failed."
],
success: [],
failed: [],
moveTo_then_Delete: []
}
function Divide_And_Empty(s, success, failed, deleteThis, regEx) {
s.filter(function(str) {
key = regEx.test(str)
if (key) {
success.push(str)
} else {
failed.push(str)
deleteThis.push(str)
}
});
//The code works correctly
//All the way up until I try to empty and/or delete an array with parameter
s = []
delete deleteThis
}
Divide_And_Empty(sentences.all, sentences.success, sentences.failed, sentences.moveTo_then_Delete, /meets/)
document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
在您的示例中,s
指的是您在参数中传递的数组。你只能改变这个数组。
如果你想从名为 s
的数组中删除所有元素,那么你应该使用
s.length = 0;
或
s.splice(0, s.length)
而不是 s = []
:
sentences = {
all: [
//1,000s of Sentences
"This sentence meets all conditions.",
"This sentence meets all conditions.",
"This sentence failed.",
"This sentence failed.",
"This sentence meets all conditions.",
"This sentence failed."
],
success: [],
failed: [],
}
function Divide_And_Empty(s, success, failed, regEx) {
s.filter(function(str) {
key = regEx.test(str)
if (key) {
success.push(str)
} else {
failed.push(str)
}
});
s.length = 0; // or s.splice(0, s.length);
}
Divide_And_Empty(sentences.all, sentences.success, sentences.failed, /meets/);
document.write("<pre>" + JSON.stringify(sentences, null, 2) + "</pre>");