简体   繁体   中英

Extract array from array in javascript

I want to extract an array from array.

In the following fiddle, I want to get the array that is starting at the first "bob" occurence and end at the last of occurence of "bob". In the fiddle I have put the whole name of the item whereas I want it to begin and end when it sees just "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"));

So what I want is something like :

array.splice(array.indexOf("bob"),array.lastIndexOf("bob"));

Is there a way to do it ?

https://jsfiddle.net/jdsy6bnm/

Thank you in advance.

I said first "bob" occurence not first occurence of "bob-04112015-172944" and same for the last.

If the goal is to grab all entries starting with the first bob and ending with the last one, including any non-bobs in-between, there's no real shortcut for finding the indexes, you have to loop:

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> 

I used splice there, which will remove the entries; you could use slice instead to keep them:

var bobs = array.slice(first, last);

If the goal is to find all bobs, and not keep entries in-between, then filter :

var bobs = array.filter(function(entry) {
    return entry.indexOf("bob") !== -1;
});

Example:

 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 won't be changed in that example. If you wanted to change it, you'd probably want to use forEach and build two new arrays:

var bobs = [];
var nonbobs = [];
array.forEach(function(entry) {
    var a = entry.indexOf("bob") === -1 ? nonbobs : bobs;
    a.push(entry);
});

First, you're using splice wrongly: The first argument is the start index, the second one is the length of the sequence, not the end index. Thus, you should do something like

array.splice(start, end - start);

To find the first and last items starting with "bob", you can iterate over the array from both sides and check on each item whether it matches your criterion and, if it does, save its index.

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);

Your updated JSFiddle

Additional detail: You might be tempted to use the function startsWith instead of indexOf , but this one's not well-supported across browsers, so I strongly suggest the latter.

Use Array.prototype.slice .

Updated 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); 

Try using while loop , 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM