如何浏览一个javascript对象 [英] how to browse a javascript object
问题描述
我是jQuery的新手。以下是包含json字典的 data
变量。
{
user:null,
currency:EUR,
balance:0,
translist:[
{trans1:东西,trans2:something2}
]
}
我的jQuery方法从Rest GET
调用
$接收到 json / Javascript对象
b $ b $ $ p $ success:function(data){
for(x in data){
console.log(x +':'+ data [x ]);
}
});
是否有任何库可以帮助解析/遍历此json对象并获得某种对象列表?我想检查一些键和它们各自的值。问题是我不需要列表中的所有键和值,还有一些值可以为空,这使我无法应用我使用SO发现的一些解决方案。
<或者通常是直接开始在成功函数中打印HTML是比较常见的?
编辑:如果是java例如它会是一个Map,我会使用迭代器遍历并查看/分析映射值,并使用我想要的值创建一些数组列表。什么和jQuery相同?
如果是java例如它会是一个Map我将使用
迭代器来遍历并查看/分析映射值,并创建
一些具有我想要的值的ArrayList。什么是jQuery中
的等价物?
你例如可以直接访问货币为 data ['currency']
。
你也可以建立一个数组:
var a = [];
for(var key in data){
.push({key:key,value:data [key]});
}
您也可以构建一些HTML并将函数应用于数据:
$ b $
$(document.body).append($(
'< table> + a.map(function(v ){
return'< tr>< td>'+ v.key +'< / td>< td>'+ v.value +'< / td>< / tr>'
})。join('')+'< / table>'
));
使用jQuery可以使相同的迭代更简单(直接从 data
):
$(document.body).append($(
'< table> + $ .map(data ,函数(value,key){
return'< tr>< td>'+ key +'< / td>< td>'+ value +'< / td>< / tr>'
})。join('')+'< / table>'
));
I'm new to jQuery. Following is the data
variable that contains a json dictionary.
{
"user":null,
"currency":"EUR",
"balance":0,
"translist": [
{ "trans1":"something","trans2":"something2" }
]
}
and my jQuery method receives a json/Javascript object
from the Rest GET
call
success: function (data){
for(x in data) {
console.log(x + ': ' + data[x]);
}
});
Is there any library that can help to parse/walk through this json object and get to some kind of objects list? I want to check some of the keys and their respective values. Problem is I don't need all the keys and values from the list and also some of the values can be null, which prevented me to apply some solutions I found using SO.
Or usually is it more common to directly start printing the HTML inside the success function?
EDIT:If it was java for example it would be a Map and I would use an iterator to walk through and see/analyse the map values, and create some array list with the values I want from it. What's equivalent of that in jQuery?
If it was java for example it would be a Map and I would use an iterator to walk through and see/analyse the map values, and create some arraylist with the values I want in it. What is the equivalent of that in jQuery?
Any javascript object can be seen as an associative map.
You can for example directly access the currency as data['currency']
.
You can also build an array :
var a = [];
for (var key in data) {
a.push({key:key, value:data[key]});
}
You could also build some HTML and apply functions to the data :
$(document.body).append($(
'<table>' + a.map(function(v){
return '<tr><td>'+v.key+'</td><td>'+v.value+'</td></tr>'
}).join('')+'</table>'
));
Using jQuery can make the same iteration simpler (working directly from data
) :
$(document.body).append($(
'<table>' + $.map(data, function(value,key){
return '<tr><td>'+key+'</td><td>'+value+'</td></tr>'
}).join('')+'</table>'
));
这篇关于如何浏览一个javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!