JavaScript access elements in array of strings that contain certain strings access elements array python,access elements array javascript,access elements

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?

Answer:1

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
Answer:2

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.

Answer:3

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;
        }
    });
Answer:4

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.

Answer:5

.sr-only the CSS class of the Bootstrap library is used to make things disappear on computer screens only. Is there any opposite class for it to make things disappear on mobiles only? I know there ...

.sr-only the CSS class of the Bootstrap library is used to make things disappear on computer screens only. Is there any opposite class for it to make things disappear on mobiles only? I know there ...

I want to make a simple audio only stream over WebRTC, using Peer.js. I'm running the simple PeerServer locally. The following works perfectly fine in Firefox 30, but I can't get it to work in Chrome ...

I want to make a simple audio only stream over WebRTC, using Peer.js. I'm running the simple PeerServer locally. The following works perfectly fine in Firefox 30, but I can't get it to work in Chrome ...

  1. chrome won't open
  2. chrome won't open on mac
  3. chrome won't play sound
  4. chrome won't update
  5. chrome wont open on pc
  6. chrome won't connect to internet
  7. chrome won't play videos
  8. chrome won't download
  9. chrome won't load pages
  10. chrome won't open pdf
  11. chrome won't open windows 10
  12. chrome won't uninstall
  13. chrome won't save passwords
  14. chrome won't download files
  15. chrome won't play audio
  16. chrome won't open on windows 7
  17. chrome won't print
  18. chrome won't sync
  19. chrome won't allow flash
  20. chrome won't open aw snap

I am working on a wordpress site that uses flexslider for displaying an image gallery. The images are loaded in the html, but are not showing in the browser. I can see the <div id="slider-1406-...

I am working on a wordpress site that uses flexslider for displaying an image gallery. The images are loaded in the html, but are not showing in the browser. I can see the <div id="slider-1406-...

  1. flexslider load images on demand
  2. flexslider load first image
  3. flexslider image loading issue

I'm trying to render objects on top of other objects. In the fiddle, get the green torus knots to render on top of the red boxes and floor. I've been trying mesh.renderDepth = 0, 1, 1000 and changes ...

I'm trying to render objects on top of other objects. In the fiddle, get the green torus knots to render on top of the red boxes and floor. I've been trying mesh.renderDepth = 0, 1, 1000 and changes ...