var mapObjects = $('#placeholdSlots div').map(function (i, n) {
var awardId = $(n).find('.dropped').attr('id');
var itemType = $(n).find('.dropped').attr('data');
var obj = {};
obj['itemId'] = parseInt(awardId) || "";
obj['type'] = itemType || ""
return obj;
});
console.log(JSON.stringify(mapObjects));
知道为什么我不能json.stringify我的对象?最后一行返回错误
Uncaught TypeError: Converting circular structure to JSON
对我而言看起来很好:
我没有看到任何关于我的对象的循环引用问题..
答案 0 :(得分:1)
$.fn.map()会返回jQuery个对象吗?请注意,如果在div.dropped
父元素中选择#placeholdSlots div
子元素,则$.map()
可以迭代div
,div.dropped
元素;在stacksnippets
$('#placeholdSlots div:not(.dropped)')
的选择器$('#placeholdSlots div')
var mapObjects = $.map($("#placeholdSlots div:not(.dropped)")
, function (n, i) {
var awardId = $(n).find(".dropped").attr("id");
var itemType = $(n).find(".dropped").attr("data");
var obj = {};
obj["itemId"] = parseInt(awardId) || "";
obj["type"] = itemType || ""
return obj;
});
console.log(JSON.stringify(mapObjects));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<div id="placeholdSlots">
<div><div id="123" class="dropped" data="abc">dropped</div></div>
<div><div id="456" class="dropped" data="def">dropped</div></div>
</div>
&#13;