本文介绍了将JSON对象成员字符串值连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
"category": [{
"id": 28,
"name": "Dogs"
},
{
"id": 14,
"name": "Cats"
},
{
"id": 878,
"name": "Sheep"
}],
我已经解析了上面的JSON(使用.ajax和jsonp作为回调),并且我想将"name"的所有值连接到一个字符串中.即狗,猫,羊".我怎样才能做到这一点?我已经尝试过对类别"和名称(即
I have the above JSON parsed (using .ajax and jsonp as callback) and I would like to join all the values of "name" into a string. i.e. "Dogs, Cats, Sheep". How can I do this? I have tried simple join on "category" and name, i.e.
var cats = categories.join(", ");
OR
var cats = categories.name.join(", ");
但是由于我们正在查看它的成员及其字符串值,所以它不起作用.
But since we are looking at it's members and their string values, it doesn't work.
推荐答案
这看起来像是$.map
的工作!
var data = {
"category": [{
"id": 28,
"name": "Dogs"
},
{
"id": 14,
"name": "Cats"
},
{
"id": 878,
"name": "Sheep"
}]
}
var cats = $.map(data.category, function(v){
return v.name;
}).join(', ');
这篇关于将JSON对象成员字符串值连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!