在JavaScript中将对象键设置为动态值

jul*_*lio 2 javascript jquery

我循环遍历jQuery选择器并尝试将表单元素的值分配给容器变量,然后可以将其作为JSON传递给进一步的操作.

假设有三组复选框,每组都在自己的div中,名为group1,group2和group3.

这是伪代码,但是像这样:

var data = {};
var groups = {
    'group1': obj with a bunch of key val pairs for checkboxes,
    'group2': another obj,
    'group3': and another obj
 }; // these define divs / groups of checkboxes

// create all the checkboxes and divs from the groups object somewhere in here

//now build a function to loop over the groups and get any checked boxes
function getInputs(){
    $.each(groups, function(index, el) {
        // this is where I am stuck
        data.index = $('#' + index + ' input:checked'); // <-- how to do this?
    });
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我想将检查项的值添加到对象中的适当项 - data例如.如果我们遍历了group1格,然后为每个组的复选框将被添加到data.group1,data.group2等等.

我如何data.index评价data.group1

zer*_*kms 6

很简单:

data[index]
Run Code Online (Sandbox Code Playgroud)

.......