[英]JavaScript loop through a JSON.stringify(data)
I want to Loop through my data and do stuff. 我想遍历我的数据并做些事情。
Following 以下
var stuff = JSON.stringify(data)
Returns me something like this: 返回我这样的东西:
{"0":"data:image/png;base64,testi,"2":"data:image/png;base64, testi2, ....
I Need to Loop through that but the Approach I did, did not work. 我需要遍历,但是我做的方法没有用。
for (var i = 0; i < stuff.length; i++) {
$('#img-thumb-id'+i).attr('src', data[i]);
}
Edit 编辑
I am using JSON.stringify
because console.log(data)
just returned me object object which I cant work with. 我正在使用
JSON.stringify
因为console.log(data)
刚刚返回了我无法使用的对象对象。
If you want to loop through data
, then you need to loop through data
. 如果要遍历
data
,则需要遍历data
。
Converting it to a JSON document will give you a string. 将其转换为JSON文档将为您提供一个字符串。 You can't (usefully) loop through that.
您不能(有用地)遍历。
Since converting it to JSON shows that it is an object, not an array, it is unlikely to have a length
, so you'll need to use a method to loop over objects . 由于将其转换为JSON表示它是一个对象,而不是数组,因此不太可能具有
length
,因此您需要使用一种方法来遍历objects 。
In order to loop over object keys you can use: 为了循环对象键,您可以使用:
var keys = Object.keys(data);
Which gives you an array with all the keys in your object. 这将为您提供一个数组,其中包含对象中的所有键。 Now you can loop the values by:
现在,您可以通过以下方式循环值:
for (var i = 0; i < keys.length; i++) {
var value = data[key];
// your code here...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.