Pushing arrays based on each row inputs dynamically

后端 未结 3 995
遇见更好的自我
遇见更好的自我 2021-01-27 05:32

Based on my code I want to push each row\'s inputs to each array. If it is row1, it should push all the input values of row 1 to array a1. The second row\'s inputs

3条回答
  •  耶瑟儿~
    2021-01-27 05:52

    I would do it like this :

    $("#check").click(function() {
      // collect data
      var rows = $("tr").get().map(r => (
        $("input", r).get().map(i => i.value)
      ));
      // print data
      $("#output").html("

    Pushed arrays:

    " + rows.map((r, i) => ( "a" + (i + 1) + ": [" + r.join(", ") + "]" )).join("
    ")); });
    
    
    1
    2

    Hints to understand the above code

    JQuery API and MDN doc :

    • http://api.jquery.com/get/#get
    • http://api.jquery.com/jQuery/#jQuery-selector-context
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    Demo of join, map and arrow functions :

    xs = ["A", "B", "C"]
    // ["A", "B", "C"]
    "[" + xs.join(", ") + "]"
    // "[A, B, C]"
    xs.map(function (x) { return parseInt(x, 16); })
    // [10, 11, 12]
    xs.map(x => parseInt(x, 16))
    // [10, 11, 12]
    xs.map((x, i) => x + i)
    // ["A0", "B1", "C2"]
    xxs = [["A", "B"], ["C", "D"]]
    // [["A", "B"], ["C", "D"]]
    xxs.map(xs => "[" + xs.join(", ") + "]")
    // ["[A, B]", "[C, D]"]
    xxs.map(xs => "[" + xs.join(", ") + "]").join("
    ") // "[A, B]
    [C, D]"

提交回复
热议问题