【问题标题】:How to filter and map array such that false values of visible are eliminated如何过滤和映射数组以消除可见的错误值
【发布时间】:2021-06-22 21:25:52
【问题描述】:

我有以下数组,下面有更多详细信息

[
    {
        "__typename": "Decor",
        "itemNum": 1,
        "purchaseDate": "2021-04-04",
        "description": "fdsf",
        "alterations": true,
        "cost": 44,
        "pieces": 3,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "df",
        "_id": "293164620554699277",
        "visible": false
    },
    {
        "__typename": "Decor",
        "itemNum": 2,
        "purchaseDate": "2021-04-02",
        "description": "Blue Jeansgfg",
        "alterations": true,
        "cost": 33,
        "pieces": 33,
        "category": "Curation",
        "purchaser": "fdsf",
        "image": "fds",
        "_id": "293164663883956749",
        "visible": false
    },
    {
        "__typename": "Decor",
        "itemNum": 3,
        "purchaseDate": "2021-03-24",
        "description": "fdsfsa",
        "alterations": true,
        "cost": 3,
        "pieces": 4,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "fds",
        "visible": false
    },
    {
        "__typename": "Decor",
        "itemNum": 4,
        "purchaseDate": "2021-03-12",
        "description": "Pair of Michaels SHoes",
        "alterations": true,
        "cost": 5,
        "pieces": 2,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "fdf",
        "visible": true
    }
]

我正在映射,然后过滤,然后映射,它主要完成我的预期,但结果有空数组值,我只想将它们一起删除。

const massaged = decorData?.findUserByID?.decor?.data?.map((item) => {
    var col = Object.values(item);

    return col
      .filter(function (hype) {
        console.log(hype);
        if (item.visible === false) {
          return false;
        }
        return true;
      })
      .map((colItem, i) => {
        return { [`col${i}`]: colItem };
      });
  });

这是结果,我想删除所有 [] 数组值。我不太清楚为什么他们只是返回空而不是一起删除

[
    [], //I WANT ALL THESE EMPTY ARRAYS REMOVED
    [], //I WANT ALL THESE EMPTY ARRAYS REMOVED
    [], //I WANT ALL THESE EMPTY ARRAYS REMOVED
    [
        {
            "col0": "Decor"
        },
        {
            "col1": 4
        },
        {
            "col2": "2021-03-12"
        },
        {
            "col3": "Pair of Michaels SHoes"
        },
        {
            "col4": true
        },
        {
            "col5": 5
        },
        {
            "col6": 2
        },
        {
            "col7": "Curation"
        },
        {
            "col8": "fdsfa"
        },
        {
            "col9": "fds.png"
        },
        {
            "col10": "294069217411465741"
        },
        {
            "col11": true
        }
    ]
]

提前谢谢

【问题讨论】:

    标签: javascript arrays loops array.prototype.map


    【解决方案1】:

    你需要先过滤整个数组,然后再将剩余的对象分成几块:

    const massaged = data
      .filter(item => item.visible)
      .map(o => Object.values(o)
        .map((v, i) => ({ [`col${i}`]: v }))
      );
    

    const data = [{
        "__typename": "Decor",
        "itemNum": 1,
        "purchaseDate": "2021-04-04",
        "description": "fdsf",
        "alterations": true,
        "cost": 44,
        "pieces": 3,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "df",
        "_id": "293164620554699277",
        "visible": false
      },
      {
        "__typename": "Decor",
        "itemNum": 2,
        "purchaseDate": "2021-04-02",
        "description": "Blue Jeansgfg",
        "alterations": true,
        "cost": 33,
        "pieces": 33,
        "category": "Curation",
        "purchaser": "fdsf",
        "image": "fds",
        "_id": "293164663883956749",
        "visible": false
      },
      {
        "__typename": "Decor",
        "itemNum": 3,
        "purchaseDate": "2021-03-24",
        "description": "fdsfsa",
        "alterations": true,
        "cost": 3,
        "pieces": 4,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "fds",
        "visible": false
      },
      {
        "__typename": "Decor",
        "itemNum": 4,
        "purchaseDate": "2021-03-12",
        "description": "Pair of Michaels SHoes",
        "alterations": true,
        "cost": 5,
        "pieces": 2,
        "category": "Curation",
        "purchaser": "fdsfa",
        "image": "fdf",
        "visible": true
      }
    ]
    
    const massaged = data
      .filter(item => item.visible)
      .map(o => Object.values(o)
        .map((v, i) => ({ [`col${i}`]: v }))
      );
      
    console.log(massaged)

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2019-09-29
      • 2013-08-29
      • 2020-11-23
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多