Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Accessing JavaScript array strings using jquery each method

I would like to access Java Script string elements (within an array) via jquery. I have written simple method that simulates what I am trying to do. This method causes RangeError exception.

function test() {
    var arr = ["janusz", "renia"];
    $(arr).each(function () {
        var  el = $(this).text();
        console.log(el);
    });
}

The error in the console looks like this:

jquery.js:1602 Uncaught RangeError: Maximum call stack size exceeded
    at Sizzle.getText (jquery.js:1602)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
    at Sizzle.getText (jquery.js:1610)
…

Why can’t I access the string via 'this' object inside each method?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

.text() is meant for retrieving text from an element. You don't have any DOM elements here, only a plain array composed of strings.

The error is thrown because getText (which jQuery calls when trying to get the text of an element) does

getText = Sizzle.getText = function( elem ) {
    var node,
        ret = "",
        i = 0,
        nodeType = elem.nodeType;

    if ( !nodeType ) {
        // If no nodeType, this is expected to be an array
        while ( (node = elem[i++]) ) {
            // Do not traverse comment nodes
            ret += getText( node );
        }
    }

resulting in infinite recursion.

While you can just refer to this in strict mode to have it refer to the string being iterated over...

'use strict';

var arr = ["janusz", "renia"];
$(arr).each(function() {
  console.log(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

It'd be far easier to ditch jQuery entirely and use plain JavaScript.

var arr = ["janusz", "renia"];
for (const item of arr) {
  console.log(item);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

or

var arr = ["janusz", "renia"];
arr.forEach((item) => {
  console.log(item);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

or

var arr = ["janusz", "renia"];
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

If you're new to programming, I'd personally recommend only using jQuery when you need to, and not trying to use it for absolutely everything (which is an unfortunately common paradigm).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda