简体   繁体   中英

access elements in array of strings that contain certain strings

I have an array that can look like this: ["whatWP", "isVBZ", "theDT", "temperatureNN", "inIN", "bostonNN"]

I want to access the element containing IN, if it exists and the next elements(s) until I reach and including an element with NN in it. and join those elements together into a string.

When I try to access the element containing IN like so, I get -1 that there is no element containing IN.

Here's how I am trying to do it:

strARR = ["whatWP", "isVBZ", "theDT", "temperatureNN", "inIN", "bostonNN"];
strARR[strARR.indexOf('IN')];

but then I get undefined because nothing at -1 exists.

How can I access the element of this array of strings if it contains IN and every element after until it matches an element containing NN, including that element? And joining those as a string?

You need a for loop for that:

var strARR = ["whatWP", "isVBZ", "theDT", "temperatureNN", "inIN", "bostonNN"];
var foundStr = null;
for (var i = 0, cString; i < strARR.length; ++i) {
     cString = strARR[i];
     if (cString.indexOf("IN") !== -1) {
         foundStr = cString;
         break;
     }
}

if (foundStr !== null) {
   /* do someting with found string */
}

strARR[strARR.indexOf('IN')] was returning a weird value because:

strARR.indexOf('IN') // returns -1 (no string "IN" in the array)
strArr[-1] // undefined

You need to evaluate each element in the array individually, not evaluate the array as a whole. Using jQuery each, you can do:

var containsIN = '';
$.each(strARR, function(){
    if($(this).indexOf('IN') !== -1){
        containsIN = $(this);
    }
});

To achieve appending or joining string until you find a string that contains 'NN' you need to modify the original if condition to:

if(containsIN === '' && $(this).indexOf('IN') !== -1)

then add another condition afterwards

if(containsIN !== ''){
    final += $(this);
}

Then, to terminate the each:

if($(this).indexOf('NN') !== -1){
   return false;
}

So, the final code should look like:

var containsIN = '';
var final = '';

$.each(strARR, function(){
        if(containsIN === '' && $(this).indexOf('IN') !== -1){
            containsIN = $(this);
        }

        if(containsIN !== ''){
            final += $(this);
        }

        if($(this).indexOf('NN') !== -1){
            return false;
        }
    });

There is no "IN" element in that array. Just an "inIN" element, which is not precisely equal. (Keep in mind, this could be an array of ANYTHING, not just strings, so it's not assumed they can check the string contents)

You'll have to loop through the strARR, using a standard for(var i = 0; i < strARR.length; i++) loop. The i variable will help you find the correct indexes. Then, for combining the results, use the Array.splice and Array.join methods. Splice can take a start index and length of items to take as arguments, and Join can take an intermediate character as an argument, like a comma, to put between them.

You can use the Array's filter() function for this. There is a polyfill available on the linked page if you need to target browsers that do not support filter() natively.

You can create any filter condition that you like and filter() will return the array elements that match your condition.

var strARR = ["whatWP", "isVBZ", "theDT", "temperatureNN", "inIN", "bostonNN"];

var strARRFiltered = strARR.filter(function(element){
  return element.indexOf("IN") !== -1;
});

alert(strARRFiltered);

Here is an example of this concept expanded a bit to include accessing multple matches and a variable filter.

To do what you're currently trying to do, the code would need to be like this:

strARR = ["whatWP", "isVBZ", "theDT", "temperatureNN", "inIN", "bostonNN"];
strARR[strARR.indexOf('inIN')];

You need to loop through each element in the array calling indexOf, rather than trying to access it as an array.

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