如何过滤和映射数组以消除可见的错误值

How to filter and map array such that false values of visible are eliminated

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

[
    {
        "__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
        }
    ]
]

提前致谢

您需要先过滤整个数组,然后再将剩余的对象拆分为多个部分:

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)