jQuery .map到数组

Sea*_*Mik 4 arrays jquery

我不知道怎么会.map到数组.我想获得除2之外的所有孩子的值,然后将其放入数组格式.

这是我正在尝试的代码:

$("#schoolSupplies").submit(function() {
    var test = $(":input").not("#numOfSupplies, #submitBtn").map(function() {
        return $(this).val();
    })
    .get()
  .join( "\", \"" );

    console.log(test);
});
Run Code Online (Sandbox Code Playgroud)

这是输出: Billy", "John

我一直在研究这个问题大约一个小时,我不知道怎么做.

Sᴀᴍ*_*ᴇᴌᴀ 11

.get()返回一个数组 - 所以只需取出.join()电话; 否则你会有一个字符串(因为那是.join()返回).

$("#schoolSupplies").submit(function() {
    var arrayOfValues = $(":input").not("#numOfSupplies, #submitBtn").map(function() {
        return $(this).val();
    })
    .get()
  //.join( "\", \"" )
    ;

    console.log('Array.isArray(arrayOfValues): ', Array.isArray(arrayOfValues)?'yes':'no', ' contents of arrayOfValues: ', arrayOfValues);
    return false; //for demonstration purposes, don't submit form normally
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="schoolSupplies">
  Supply Name: <input id="name" type="text" value="Tables" /><br />
  
  Student Name: <input id="studentName" type="text" value="Bobby"/><br />
  # of Supplies: <input id="numOfSupplies" type="number" value="3" /><br />
  <input type="submit" id="submitBtn" />
  </form>
Run Code Online (Sandbox Code Playgroud)