我在数组中有对象值。我想创建一个如下所示的JSON结构。我在下面发布了HTML和JS,但我不知道如何编写javascript来创建这个JSON结构。请帮助我。你能纠正我的javascript代码吗?
{
"family1": [{
"checkedboxvalue": "test1"
}, {
"checkedboxvalue": "test2"
}, {
"checkedboxvalue": "test3"
}],
"family2": [{
"checkedboxvalue": "test5"
}, {
"checkedboxvalue": "test6"
}, {
"checkedboxvalue": "test7"
}],
"family3": [{
"checkedboxvalue": "test9"
}, {
"checkedboxvalue": "test10"
}, {
"checkedboxvalue": "test11"
}]
}
<div class="family1">
<input type="checkbox" value="test1"/>
<input type="checkbox" value="test2"/>
<input type="checkbox" value="test3"/>
<input type="checkbox" value="test4"/>
</div>
<div class="family2">
<input type="checkbox" value="test5"/>
<input type="checkbox" value="test6"/>
<input type="checkbox" value="test7"/>
<input type="checkbox" value="test8"/>
</div>
<div class="family3">
<input type="checkbox" value="test9"/>
<input type="checkbox" value="test10"/>
<input type="checkbox" value="test11"/>
<input type="checkbox" value="test12"/>
</div>
这是我的JavaScript。这不能正常工作。你能发现那个错误吗?
var CollectionArray = [];
var objects = ['family1', 'family2', 'family3'];
for (var i = 0; i <= objects.length; i++) {
var familyObject = objects[i];
var checkedboxvalue = "";
$('.' + familyObject + ' input[type="checkbox"]').each(function() {
if ($(this).is(":checked")) {
checkedboxvalue = $(this).val();
CollectionArray.push({
familyObject: {
"checkedboxvalue": checkedboxvalue
}
});
}
});
console.log(CollectionArray);
}
答案 0 :(得分:0)
尝试像这样的
strcmp