我想从数组中提取数组。
在下面的小提琴中,我希望得到的数组从第一个“bob”出现开始,并在“bob”出现的最后一个结束。在小提琴中我放了项目的全名,而我希望它开始和结束时只看到“bob”。
var array = ["rrrrrrr-18082015-144751", "rrrrrrr-18082015-145552", "rrrrrrr-30072015-182930", "rrrrrrr-30072015-184607", "bob-04112015-172944", "bob-04112015-173503", "bob-06112015-175858", "bob-06112015-180111", "bob-09112015-092450", "bob-09112015-133119", "bob-09112015-135310", "bob-09112015-143033", "bob-10112015-094836", "bob-10112015-102459", "ggggggggggg-28082015-113014"];
var x = array.splice(array.indexOf("bob-04112015-172944"),array.lastIndexOf("bob-10112015-102459"));
所以我想要的是:
array.splice(array.indexOf("bob"),array.lastIndexOf("bob"));
有办法吗?
https://jsfiddle.net/jdsy6bnm/
提前谢谢。
答案 0 :(得分:1)
我首先说“bob”出现不是“bob-04112015-172944”的第一次出现,并且最后一次出现。
如果目标是获取从第一个bob开始到最后一个bob结束的所有条目,包括中间的任何非bobs,那么找到索引没有真正的快捷方式,你有循环:
var first = -1, last = -1;
array.forEach(function(entry, index) {
if (entry.indexOf("bob") !== -1) {
if (first === -1) {
first = index;
}
last = index;
}
});
var bobs = array.splice(first, last - first + 1);
var array = ["rrrrrrr-18082015-144751", "rrrrrrr-18082015-145552", "rrrrrrr-30072015-182930", "rrrrrrr-30072015-184607", "bob-04112015-172944", "bob-04112015-173503", "bob-06112015-175858", "bob-06112015-180111", "bob-09112015-092450", "bob-09112015-133119", "bob-09112015-135310", "bob-09112015-143033", "bob-10112015-094836", "bob-10112015-102459", "ggggggggggg-28082015-113014"];
var first = -1, last = -1;
array.forEach(function(entry, index) {
if (entry.indexOf("bob") !== -1) {
if (first === -1) {
first = index;
}
last = index;
}
});
var bobs = array.splice(first, last - first + 1);
snippet.log("updated array: " + JSON.stringify(array));
snippet.log("bobs: " + JSON.stringify(bobs));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
我在那里使用了splice
,这将删除条目;您可以使用slice
来保留它们:
var bobs = array.slice(first, last);
如果目标是查找所有bobs,而不是中间的条目,那么filter
:
var bobs = array.filter(function(entry) {
return entry.indexOf("bob") !== -1;
});
示例:
var array = ["rrrrrrr-18082015-144751", "rrrrrrr-18082015-145552", "rrrrrrr-30072015-182930", "rrrrrrr-30072015-184607", "bob-04112015-172944", "bob-04112015-173503", "bob-06112015-175858", "bob-06112015-180111", "bob-09112015-092450", "bob-09112015-133119", "bob-09112015-135310", "bob-09112015-143033", "bob-10112015-094836", "bob-10112015-102459", "ggggggggggg-28082015-113014"];
var bobs = array.filter(function(entry) {
return entry.indexOf("bob") !== -1;
});
snippet.log(JSON.stringify(bobs));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
在该示例中,不会更改
array
。如果你想改变它,你可能想要使用forEach
并构建两个新数组:
var bobs = [];
var nonbobs = [];
array.forEach(function(entry) {
var a = entry.indexOf("bob") === -1 ? nonbobs : bobs;
a.push(entry);
});
答案 1 :(得分:1)
首先,您错误地使用splice
:第一个参数是起始索引,第二个参数是序列的长度,而不是结束索引。因此,您应该执行类似
array.splice(start, end - start);
要查找以&#34; bob&#34;开头的第一个和最后一个项目,您可以从两侧迭代数组并检查每个项目是否符合您的标准,如果匹配,则保存其索引。
var array = ["rrrrrrr-18082015-144751", "rrrrrrr-18082015-145552", "rrrrrrr-30072015-182930", "rrrrrrr-30072015-184607", "bob-04112015-172944", "bob-04112015-173503", "bob-06112015-175858", "bob-06112015-180111", "bob-09112015-092450", "bob-09112015-133119", "bob-09112015-135310", "bob-09112015-143033", "bob-10112015-094836", "bob-10112015-102459", "ggggggggggg-28082015-113014"];
var first = -1, last = -1;
for (var i = 0; i < array.length; i++) {
if (first < 0 && array[i].indexOf('bob') === 0)
first = i;
if (last < 0 && array[array.length - i - 1].indexOf('bob') === 0)
last = array.length - i - 1;
}
var x = array.splice(first, last - first);
其他细节:
你可能会想要使用startsWith
而不是indexOf
这个函数,但这个浏览器并没有得到很好的支持,所以我强烈建议使用后者。
答案 2 :(得分:0)
使用Array.prototype.slice
。
更新了JSFiddle:https://jsfiddle.net/jdsy6bnm/1/
var array = [
"rrrrrrr-18082015-144751", "rrrrrrr-18082015-145552",
"rrrrrrr-30072015-182930", "rrrrrrr-30072015-184607",
"bob-04112015-172944", "bob-04112015-173503",
"bob-06112015-175858", "bob-06112015-180111",
"bob-09112015-092450", "bob-09112015-133119",
"bob-09112015-135310", "bob-09112015-143033",
"bob-10112015-094836", "bob-10112015-102459",
"ggggggggggg-28082015-113014"
];
var array = array.slice(
array.indexOf('bob-04112015-172944'),
array.lastIndexOf("bob-10112015-102459")
);
console.log(array);
&#13;
答案 3 :(得分:0)
尝试使用while
循环,RegExp.prototype.test()
var array = ["rrrrrrr-18082015-144751"
, "rrrrrrr-18082015-145552"
, "rrrrrrr-30072015-182930"
, "rrrrrrr-30072015-184607"
, "bob-04112015-172944"
, "bob-04112015-173503"
, "bob-06112015-175858"
, "bob-06112015-180111"
, "bob-09112015-092450"
, "bob-09112015-133119"
, "bob-09112015-135310"
, "bob-09112015-143033"
, "bob-10112015-094836"
, "bob-10112015-102459"
, "ggggggggggg-28082015-113014"];
var res = []
, i = 0
, re = /bob/;
while (i < array.length) {
if (re.test(array[i]) && re.test(array[i + 1])) {
res.push(array[i], array[i + 1])
} else {
if (/bob/.test(array[i]) && !/bob/.test(array[i + 1])
&& /bob/.test(array[i - 1])) {
break;
}
}
++i;
}
console.log(res)