将未关联的数组与序列化的名称/值对合并

时间:2016-03-03 18:53:17

标签: javascript jquery

除了迭代未关联的数组并手动添加它们之外,如何将未关联的数组与序列化的名称/值对合并?

//I select some values
var data = $('#mytable input:checked');
console.log(data);  //Object[input attribute value = "1", input attribute value = "3"]

//I get some extra values I wish to send to the server
var types = data.map(function() {
    return $(this).data("types");
}).get();
console.log(types); // ["type1", "type3"]

//I serialize data
data = data.serializeArray();
console.log(data);  // [Object { name="id[]",  value="1"}, Object { name="id[]",  value="3"}]

//I add an extra value
data.push({
    name: "task",
    value: 'restoreRecord'
})
console.log(data);  // [Object { name="id[]",  value="1"}, Object { name="id[]",  value="3"}, Object { name="task",  value="restoreRecord"}]

/*
How do I merge data and types so that I come up with the following:
[
Object { name="id[]",    value="1"},
Object { name="id[]",    value="3"},
Object { name="types[]", value="type1"},
Opject { name="types[]", value="type3"},
Object { name="task",    value="restoreRecord"}
]
*/

1 个答案:

答案 0 :(得分:0)

您可以在create上运行for循环,并将其中的值添加到def new key = SecureRandom end def create unless params[:key] != key end end

types