使用.each()动态创建JSON数组

Jac*_*anz 2 jquery json

我正在尝试动态创建一个JSON数组.在网站中,有一个动态数量的<div id ="#selected">,我需要获取所有值并创建一个JSON数组.

我遇到过.push()功能,但我无法弄明白.

<!-- there could be a million of these, or only one... each value is unique though -->
<div id="selected" value="5|3"></div>
<div id="selected" value="3|65"></div>


function json_array_selected() {

var JSon = {};
$('div#selected').each(function() {
        // let's first split the given values
        var Split = $(this).attr('value');
        Split = Split.split('|');
        var Type = Split[0];
        Value = Split[1];

        // now let's set up our Json array... using the value = type way, there should never be
        // any repeating 
        JSon.Value = Type;

});
return JSon;
}
Run Code Online (Sandbox Code Playgroud)

fca*_*ran 6

代替

JSon.Value = Type;
Run Code Online (Sandbox Code Playgroud)

尝试

JSon[Value] = Type;
Run Code Online (Sandbox Code Playgroud)

或者你将永远覆盖一个名为"价值"的键