Pregunta

I'm just starting to learn underscore js, just want to see the result of this simple exercise, but the console.log is not working for me, below the code

    <script>

        var Array = [1,2,3,'hello',5]

         function StringArray(){
          for(var i = 0;i<Array.length;i++)
            if(typeof Array[i] ==='string')
           _.each(Array[i]);
         }

    </script>

 console.log(StringArray());
¿Fue útil?

Solución

You need to specify the iterator function as the second argument to _each, i.e.:


var arr = [1,2,3,'hello',5]

function StringArray() { for (var i = 0; i < arr.length; i++) { if(typeof arr[i] ==='string') { _.each(arr[i], function(element, index, list) { console.log(element); }); } } }

StringArray();

I also changed your var name from Array to arr to avoid overwriting the built-in Array.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top