【问题标题】:List of numbers to list of numbers数字列表到数字列表
【发布时间】:2019-06-30 20:43:40
【问题描述】:

我有一个数字列表,格式如下:

[[1,2],[3,4],[5,1]]

我期待得到下一个输出:

[1,2,3,4,5]

这是我当前方法的一个示例:

<!DOCTYPE html>
<html>
<head>
    <title>Expected - [1,2,3,4,5,6,7,8,9,10]</title>
</head>
<body>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            // Expected - [1,2,3,4,5,6,7,8,9,10];
            var array_of_arrays = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
            console.log(array_of_arrays);

            // Section 1 - code does what I expect. I would like something like Section 2.
            var array_of_values = [];
            array_of_arrays.map((x) => {
                x.map((y) => {
                    if (array_of_values.indexOf(y) == -1)
                        array_of_values.push(y)
                })
            });
            console.log(array_of_values);

            // Section 2 - This code does not do what I expect.
            var resp = array_of_arrays.map(x => x.map(y => y));
            console.log(resp);
        });
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript jquery arrays filter arrow-functions


    【解决方案1】:

    您可以使用flatMap 来实现这一点。它根据提供的回调函数展平(取消嵌套)数组。使用过滤器过滤掉重复项并使用 sort() 对它们进行排序

    var a=[[1,2],[3,4],[5,1]].flatMap((x)=>x);
    var arr=[];
    console.log(a.filter((e)=>arr.indexOf(e)==-1?arr.push(e):false).sort());

    【讨论】:

    • 他说他只期望 [1,2,3,4,5],大概是因为重复只出现一次
    【解决方案2】:

    您可以使用 Array.prototype.reduce() 和使用 Set

    let arr = [[1,2],[3,4],[5,1]]
    //flatting array
    arr = arr.reduce((ac,item) => ([...ac,...item]),[])
    //remove dupliactes
    arr = [...new Set(arr)];
    console.log(arr);

    【讨论】:

      【解决方案3】:

      一种方法可以使用reduce()set 作为accumulator

      let input = [[1,2],[3,4],[5,1]];
      
      let res = input.reduce((acc, a) =>
      {
          a.forEach(x => acc.add(x));
          return acc;
      }, new Set());
      
      console.log(Array.from(res));
      .as-console {background-color:black !important; color:lime;}
      .as-console-wrapper {max-height:100% !important; top:0;}

      另一种方法是使用新的flat() 方法:

      let input = [[1,2],[3,4],[5,1]];
      let res = new Set(input.flat());
      console.log(Array.from(res));
      .as-console {background-color:black !important; color:lime;}
      .as-console-wrapper {max-height:100% !important; top:0;}

      【讨论】:

        【解决方案4】:

        您可以创建一个distinct 过滤器并使用flatMap

        const distinct = (value, index, self) => {
          return self.indexOf(value) == index;
        }
        
        console.log(
          [[1,2],[3,4],[5,6],[7,8],[9,10],[1,2],[3,4],[5,6],[7,8],[9,10]]
          .flatMap(x => x)
          .filter(distinct)
        );

        【讨论】:

          【解决方案5】:

          这是你想要的吗?

          阅读评论

          var array_of_arrays = [[1, 2],[3, 4], [5, 6], [7, 8], [9, 10], [1, 2],[3, 4], [5, 6], [7, 8], [9, 10]];
          // flat the array and then select distinct after that sort the array out
          var arr = array_of_arrays.flatMap((x)=>x).filter((x, i, a) => a.indexOf(x) == i).sort((a,b)=> a-b)
          
          console.log(arr)

          【讨论】:

            【解决方案6】:

            您可以使用 reduce 并将 Set 对象作为初始值传递,Set 用于唯一值。

             var list = [[1,2],[3,4],[5,1]];
             var arr = list.reduce((acc, c)=>{ c.map((a)=>{  acc.add(a) }); return acc; }, new Set());
            

            【讨论】:

              猜你喜欢
              • 2022-06-14
              • 2021-04-10
              • 2021-03-17
              • 2017-08-29
              • 2022-01-08
              • 2015-12-17
              • 1970-01-01
              • 2019-03-25
              • 1970-01-01
              相关资源
              最近更新 更多