search
HomeWeb Front-endJS TutorialJQuery $.each traverses JavaScript array object instances_jquery

View a simple jQuery example to iterate over a JavaScript array object.

var json = [
{"id":"1","tagName":"apple"},
{"id":"2","tagName":"orange"},
{"id":"3","tagName":"banana"},
{"id":"4","tagName":"watermelon"},
{"id":"5","tagName":"pineapple"}
];

$.each(json, function(idx, obj) {
alert(obj.tagName);
});

The above code snippet works fine, prompting “apple”, “orange”…etc., as expected.
Problem: JSON string

The following example declares a JSON string (enclosed in single or double quotes) directly.

var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},
{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},
{"id":"5","tagName":"pineapple"}]';

$.each(json, function(idx, obj) {
alert(obj.tagName);
});

In Chrome it shows the following error in the console:

Uncaught TypeError: Cannot use 'in' operator to search for '156'
in [{"id":"1","tagName":"apple"}...

Solution: Convert JSON string to JavaScript object.
To fix it, convert it to a JavaScript object via standard JSON.parse() or jQuery's $.parseJSON .

var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},
{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},
{"id":"5","tagName":"pineapple"}]';

$.each(JSON.parse(json), function(idx, obj) {
alert(obj.tagName);
});

//or 

$.each($.parseJSON(json), function(idx, obj) {
alert(obj.tagName);
});
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

javascript怎么给数组中增加元素javascript怎么给数组中增加元素Nov 04, 2021 pm 12:07 PM

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

Java如何遍历文件夹并获取所有文件名Java如何遍历文件夹并获取所有文件名Mar 29, 2024 pm 01:24 PM

Java是一种流行的编程语言,具有强大的文件处理功能。在Java中,遍历文件夹并获取所有文件名是一种常见的操作,可以帮助我们快速定位和处理特定目录下的文件。本文将介绍如何在Java中实现遍历文件夹并获取所有文件名的方法,并提供具体的代码示例。1.使用递归方法遍历文件夹我们可以使用递归方法来遍历文件夹,递归方法是一种自身调用自身的方式,可以有效地遍历文件夹中

PHP glob()函数使用示例:遍历指定文件夹中的所有文件PHP glob()函数使用示例:遍历指定文件夹中的所有文件Jun 27, 2023 am 09:16 AM

PHPglob()函数使用示例:遍历指定文件夹中的所有文件在PHP开发中,经常需要遍历指定文件夹中的所有文件,以实现文件批量操作或读取。PHP的glob()函数正是用来实现这种需求的。glob()函数可以通过指定一个通配符匹配模式,来获取指定文件夹中符合条件的所有文件的路径信息。在这篇文章中,我们将会演示如何使用glob()函数来遍历指定文件夹中的所有文件

Java Iterator 和 Iterable 的深入比较:优缺点分析Java Iterator 和 Iterable 的深入比较:优缺点分析Feb 19, 2024 pm 04:20 PM

概念差异:Iterator:Iterator是一个接口,代表一个从集合中获取值的迭代器。它提供了MoveNext()、Current()和Reset()等方法,允许你遍历集合中的元素,并对当前元素进行操作。Iterable:Iterable也是一个接口,代表一个可迭代的对象。它提供了Iterator()方法,用于返回一个Iterator对象,以便于遍历集合中的元素。使用方式:Iterator:要使用Iterator,需要先获得一个Iterator对象,然后调用MoveNext()方法来移动到下一

Python 3.x 中如何使用os模块遍历目录中的文件Python 3.x 中如何使用os模块遍历目录中的文件Jul 29, 2023 pm 02:57 PM

Python3.x中如何使用os模块遍历目录中的文件在Python中,我们可以使用os模块来进行文件和目录的操作。os模块是Python标准库中的一个重要模块,提供了许多和操作系统相关的功能。在本文中,我们将介绍如何使用os模块来遍历一个目录中的所有文件。首先,我们需要导入os模块:importos接下来,我们可以使用os.walk()函数来遍历目录。

在C++中递归插入和遍历链表在C++中递归插入和遍历链表Sep 10, 2023 am 09:21 AM

我们得到了用于形成链表的整数值。任务是使用递归方法先插入然后遍历单链表。在末尾递归添加节点如果head为NULL→将节点添加到head否则添加到head(head→next)递归遍历节点如果head为NULL→退出否则打印(head→next)示例输入−1-2-7-9-10输出输出strong>−链表:1→2→7→9→10→NULL输入−12-21-17-94-18输出−链表:12→21→17→94→18→NULL下面程序中使用的方法如下在这种方法中,我们将使用函数添加节点并遍历单链表并递

yii如何将对象转化为数组或直接输出为json格式yii如何将对象转化为数组或直接输出为json格式Jan 08, 2021 am 10:13 AM

yii框架:本文为大家介绍了yii将对象转化为数组或直接输出为json格式的方法,具有一定的参考价值,希望能够帮助到大家。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools