【问题标题】:Merge two arrays with input fields together将两个带有输入字段的数组合并在一起
【发布时间】:2020-09-13 01:00:50
【问题描述】:

我想将两个数组合并在一起。我知道有 $.merge 但这不起作用。

这是我的数组:

S (1)
0 S (2)
    <input type="text" name="f_name">
    <input type="text" name="l_name">
1 S (2)
    <input type="text" name="f_name">
    <input type="text" name="l_name">

如何将它们组合起来,结果是:

0 S (4)
    <input type="text" name="f_name">
    <input type="text" name="l_name">
    <input type="text" name="f_name">
    <input type="text" name="l_name">

PS:我得到了第一个数组使用

selected.forEach((a, i) => {
  arr[i] = a.map(function() {
    return $(this).find('input');
  });
});

【问题讨论】:

    标签: jquery arrays loops foreach


    【解决方案1】:

    使用Array.prototype.flat() 方法。 看下面的例子,如果depth是未知的,你可以指定Infinity

    const input = [1, 2, [3, 4, [5, 6]]];
    const flattenedArr = input.flat(3);
    console.log(flattenedArr);

    【讨论】:

    • 感谢您的分析。但这似乎不适用于选定的元素。还是我做错了什么?我刚刚将 arr.flat(2) 添加到我的代码中。
    【解决方案2】:

    使用concat(...) 方法。如下例。

    var arr1 = [0, 1, 2];
    var arr2 = [3, 4, 5];
    var All = arr1.concat(arr2); 
    console.log(All)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2018-11-23
      • 2020-07-07
      相关资源
      最近更新 更多