【发布时间】:2017-11-09 15:17:19
【问题描述】:
我有以下代码:
var fieldsJson = formBuilder.actions.getData('json', true);
console.log(fieldsJson);
var nameArr = fieldsJson.map(function (item) { return item.name });
但我不断收到错误:
fieldsJson.map is not a function
控制台清楚地显示 fieldsJson 是一个对象数组,如下所示:
[
{
"type": "text",
"label": "Text Field",
"className": "form-control",
"name": "testfield",
"subtype": "text",
"customField": true
},
{
"type": "text",
"label": "Text Field",
"className": "form-control",
"name": "testfield",
"subtype": "text",
"customField": true
}
]
那为什么地图函数会报错呢?
【问题讨论】:
-
你确定这是一张地图而不是一个 JSON 字符串吗?
console.log(typeof fieldsJson) -
如果 typeof(fieldsJson) 返回 'object' 它应该可以工作。你能在小提琴中重现错误吗?
-
fieldsJSON.length呢? -
@mikeb 你是对的,它是一个 json 字符串。如何将其转换为对象数组?
标签: jquery array.prototype.map