javascript - How to parse an array inside an object
问题描述
Here is my array which I need to parse, as I am parsing array it is showing me undefined
in console.
var jsonString = {
"results": [{
"cc_emails": [
"test@test.com",
"test1@test.com",
"tst2@tst2.com"
],
"name": "test",
"email": "testemail",
"email_config_id": 14000037621,
"priority": 2,
"product_id": null,
"created_at": "2021-10-05T22:01:17Z",
"updated_at": "2021-10-05T22:05:05Z"
}]
}
I need to correct the above error and show priority
. Here is my code which consists of forEach()
loop with which I was iterating the array.
// I used foreach here.
$.each(jsonString, function (index, val)
{
alert jsonString[index].priority;
});
解决方案
your jsonString
is an object that contains an array results
.
You need to apply forEach
on the result instead of the object.
var jsonString = {
"results": [{
"cc_emails": [
"test@test.com",
"test1@test.com",
"tst2@tst2.com"
],
"name": "test",
"email": "testemail",
"email_config_id": 14000037621,
"priority": 2,
"product_id": null,
"created_at": "2021-10-05T22:01:17Z",
"updated_at": "2021-10-05T22:05:05Z"
}]
}
jsonString.results.forEach(item => {
console.log(item.priority)
})
推荐阅读
- python - 要检查 Pandas Dataframe 列的 TRUE/FALSE,如果 TRUE 检查另一列是否满足条件并生成具有值 PASS/FAIL 的新列
- python - 使用 Amazon Textract 从多页文档 PDF 中同步检测和分析文本
- c# - 如何停止在 5 秒内多次调用方法?
- symfony - 如何正确实现与 Doctrine 的“朋友关系”
- .net - .Net Core Avalonia 中如何支持汉字?
- php - 如何从 Ramsey\Uuid\Codec\StringCodec 中提取 uuid?
- javascript - 在 NodeJS 中等待数据返回而不是休眠
- laravel - 在 laravel-nova 中每小时删除所有行并将数据从外部 api 插入到 sql db(使用 laravel 任务调度程序)
- javascript - 在 HTML5 视频播放器中使用 Adocean
- html - 具有不透明度的重叠元素的 Z-index 位置