Js遍历Josn对象(内容对比页实现思路)
更好的遍历Josn的方法,利用jquery的each方法:
var arr1 = [ "one", "two", "three", "four", "five" ];
$.each(arr1, function(){
alert(this);
});
输出:one two three four five
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){
alert(item[0]);
});
输出:1 4 7
var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});
输出:1 2 3 4 5
var obj = { "one": ["1", "2", "3"], "two": ["1", "2", "3"], "three": ["1", "2", "3"], "four": ["1", "2", "3"], "five": ["1", "2", "3"] };
$.each(obj, function (key, val) {
alert(val[2]);
});
写了几个例子,运行一下看看就明白啦!很简单吧!