为了账号安全,请及时绑定邮箱和手机立即绑定

减少数组数组并从 Javascript 数据中删除 null

减少数组数组并从 Javascript 数据中删除 null

一只斗牛犬 2023-09-21 18:24:22
我很难通过使用 Javascript 去除空值和嵌入数组来“压缩”语法结果。我正在使用 Nearley 语法检查器,它可以在句子匹配后运行 JS 函数。不幸的是,您得到的完整解析结果是一系列数组。以下是输出示例[   [      [         [            [               [                  [ [ 'climb'], [ [ null, 'to' ] ] ],                  [ [ null, [ 'alt' ] ] ],                  [ 332, [ null, [ 'km' ] ] ]               ]            ],            [ null ]         ]      ]   ]]我想删除所有这些null值,并可能将所有这些数组“压缩”为以下形式:[   [ 'climb', 'to' ],   [ 'alt', 332, 'km' ]]或者类似的东西。我尝试过使用各种filter方法但没有成功。
查看完整描述

4 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

您可以使用相同的方法过滤地图并平坦所有数组。


const

    filter = array => array.flatMap(v => Array.isArray(v)

        ? filter(v)

        : v === null

            ? []

            : v

    ),

    array = [[[[[[[['climb'], [[null, 'to']]], [[null, ['alt']]], [332, [null, ['km']]]]], [null]]]]],

    result = filter(array);


console.log(result);

更短的方法


const

    filter = array => array

        .flat(Infinity)

        .filter(v => v !== null),

    array = [[[[[[[['climb'], [[null, 'to']]], [[null, ['alt']]], [332, [null, ['km']]]]], [null]]]]],

    result = filter(array);


console.log(result);


查看完整回答
反对 回复 2023-09-21
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

真的吗?...那怎么样['climb', 'to', 'alt', 332, 'km']?– 彼得·塞利格

那好极了。某种逻辑分组会更好,但嘿,如果没有的话......

...

const sample = [[[[

  [[

    [ [ 'climb'], [ [ null, 'to' ] ] ],

    [ [ null, [ 'alt' ] ] ],

    [ 332, [ null, [ 'km' ] ] ]

  ]],

  [ null ]

]]]];


function flatOut(list, item) {

  if (Array.isArray(item)) {

    item = item.reduce(flatOut, []);

  }

  return list.concat(item);

}

const result = sample

  .reduce(flatOut, [])

  .filter(elm => (elm != null)); // non strict equality

  //...in order to skip both values, undefined and null.


console.log('result :', result);


查看完整回答
反对 回复 2023-09-21
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

一条线解决方案:


1.转换为字符串 2.拆分为平面数组 3.删除空值


例子:


var arr = [

    [

        [

            [

                [

                    [

                        [['climb'], [[null, 'to']]],

                        [[null, ['alt']]],

                        [332, [null, ['km']]]

                    ]

                ],

                [null]

            ]

        ]

    ]

];




var res = arr.toString().split(",").filter(item => item);



console.log(res);


查看完整回答
反对 回复 2023-09-21
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

let resArr = [];

  const findLoc = (arr, loc = []) => {

    arr.map((d, i) => {

      if (Array.isArray(d)) {

        findLoc(d, [ ...loc, i ]);

      } else {

        if (d !== null) {

          // console.log(d, loc);

          resArr.push([...loc, d])

        }

      }

    })

}


  const a = [

     [

        [

           [

              [

                 [

                    [ [ 'climb'], [ [ null, 'to' ] ] ],

                    [ [ null, [ 'alt' ] ] ],

                    [ 332, [ null, [ 'km' ] ] ],

                    [ 56, [ null, null, [ [ [8] ] ] ] ]

                 ]

              ],

              [ null ]

           ]

        ]

     ]

  ];

  findLoc(a);

  let finalIndex = [...resArr.reverse()[0]];

  finalIndex.splice(finalIndex.length -1 , 1);

  finalIndex = resArr[0].indexOf(Math.max(...finalIndex));

  const finalObj = {};

  resArr.forEach((d) => {

    finalObj[d[finalIndex]] = finalObj[d[finalIndex]] ? [...finalObj[d[finalIndex]], d[d.length -1]] : [d[d.length -1]]

  });

  console.log(Object.values(finalObj));

  

  // [ [ "to", "climb" ], [ "alt" ], [ "km", 332 ], [ 8, 56 ] ]

改变输入并测试,

使用矩阵逻辑我们可以激活它


查看完整回答
反对 回复 2023-09-21
  • 4 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信