0
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));

Any idea why I can't json.stringify my object? The last line return an error of

Uncaught TypeError: Converting circular structure to JSON

For me it look fine :

enter image description here

I didn't see any circular reference problem with my object..

3
  • Please edit your question to explain where you're apparently running into some sort of problem related to circular references. Thanks for improving the question's reference value and making it more answerable! Commented Mar 24, 2015 at 2:47
  • Your picture didn't come through - please re-attach. Commented Mar 24, 2015 at 3:05
  • Try $.map() , $.fn.map() returning jQuery.fn() ? Commented Mar 24, 2015 at 3:24

1 Answer 1

1

$.fn.map() returns jQuery object ? Note, if selecting div.dropped child element within #placeholdSlots div parent element , $.map() could iterate both div , div.dropped elements ; substituted selector $('#placeholdSlots div:not(.dropped)') for $('#placeholdSlots div') at stacksnippets

Try utilizing $.map() , returns Array,

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>

Sign up to request clarification or add additional context in comments.

6 Comments

with this I got this : [{"itemId":null,"type":null},{"itemId":null,"type":null},{"itemId":null,"type":null},{"itemId":null,"type":null},{"itemId":null,"type":null},{"itemId":null,"type":null}]
Not certain about html ? , not appear included at OP ? Can include html at OP ? See updated post.
why not()? isn't that excluded everything?
At stacksnippets , selector #placeholdSlots div would select all div elements - including both <div> without attributes and <div class="dropped"> - resulting innull being returned for <div> at var awardId , var itemType . :not(.dropped) filters parent element <div> not having class .dropped . Can include html at OP ?
it worked, just that I'm not so understand how map work.. my html is ` <div class="placehold" data-sort-helper="2" id="placehold2"></div> ` and i select using $('#placeholdSlots .placehold') but not sure why I need to use` .not()`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.