具有随机键值的Json对象

时间:2016-03-04 07:34:03

标签: json image loops object

我正在开发一个应用程序,我将图像拖放到网格中,然后在删除后我创建了一个json对象,我将这些拖放图像添加到json对象中,有一个144平方的网格。

我的json对象是通过条目创建的:

27: Object
51: Object
54: Object
75: Object
99: Object
123: Object
125: Object

如何在jquery中循环我的确切键号以与其他json对象匹配?

2 个答案:

答案 0 :(得分:0)

示例示例:

var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };

jQuery.each( arr, function( i, val ) {
  $( "#" + val ).text( "Mine is " + val + "." );

  // Will stop running after "three"
  return ( val !== "three" );
});

jQuery.each( obj, function( i, val ) {
  $( "#" + i ).append( document.createTextNode( " - " + val ) );
});

输出:

Mine is one. - 1
Mine is two. - 2
Mine is three. - 3
- 4
- 5

答案 1 :(得分:0)

你可以像这样循环对象的属性:

var obj = {1: 'val1', 2:'val2', 3:'val3' }
for (var param in obj) {
    if (obj.hasOwnProperty(param))
        console.log(obj[param])
}

这将打印出来:

val1
val2
val3