Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!