The following code produces an array based on values of the designated inputs:
以下代码根据指定输入的值生成一个数组:
<input value="jan, feb, mar">
<input value="apr, may, jun">
<input value="jul, aug, mar">
####
anArray = []
$("input").each ->
tv = $(this).val()
anArray.push(tv)
console.log anArray
>>> ["jan, feb, mar", "apr, may, jun", "jul, aug, sep"]
How can I make it to be a set of arrays wrapped in another array?
如何使它成为包含在另一个数组中的一组数组?
[ ["jan, feb, mar"], ["apr, may, jun"], ["jul, aug, sep"] ]
I also somewhat managed it to do as a set of objects, but I don't need a key at all. Maybe I can strip this object of key
, leaving only value
?
我也有点把它作为一组对象来管理,但我根本不需要密钥。也许我可以剥掉这个钥匙的对象,只留下价值?
content = $("input")
object = $.map content, (x) ->
'key': $(x).val()
console.log JSON.stringify(object)
>>> [{"key":"jan, feb, mar"},{"key":"apr, may, jun"},{"key":"jul, aug, mar"}]
In the end I'm going to post this data via JSON to the server, so all what I really need is to meet controller expectations of the data format where each input's value will be grouped together and separated with coma.
最后,我将通过JSON将这些数据发布到服务器,所以我真正需要的是满足控制器对数据格式的期望,其中每个输入的值将被组合在一起并用昏迷分隔。
2 个解决方案
#1
1
Instead of pushing the string, push an array with the strings:
不是推动字符串,而是使用字符串推送数组:
anArray = []
$("input").each ->
tv = $(this).val()
anArray.push([tv])
console.log anArray
#2
-1
use the split
function:
使用拆分功能:
anArray = []
$("input").each(function(tv){
tv = $(this).val()
anArray.push(tv.split(","))
})
document.write(JSON.stringify(anArray));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="jan, feb, mar">
<input value="apr, may, jun">
<input value="jul, aug, mar">
<br>
#1
1
Instead of pushing the string, push an array with the strings:
不是推动字符串,而是使用字符串推送数组:
anArray = []
$("input").each ->
tv = $(this).val()
anArray.push([tv])
console.log anArray
#2
-1
use the split
function:
使用拆分功能:
anArray = []
$("input").each(function(tv){
tv = $(this).val()
anArray.push(tv.split(","))
})
document.write(JSON.stringify(anArray));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="jan, feb, mar">
<input value="apr, may, jun">
<input value="jul, aug, mar">
<br>