为什么在使用debug时过滤器输出不同?

时间:2019-04-01 08:07:10

标签: javascript json

我想使用'filter'过滤一些数据,但是输出数据正在更改,并且当我尝试调试以查找原因时,输出数据是正确的...

我已经尝试使用'JSON.parse(JSON.stringify(data))'使它正常工作,但是TAT失败。

let needGenerateDate = sDate.data.filter(item => {
  return item.week !== week;
});
console.log("needGenerateDate", needGenerateDate);

JSON.stringify(sDate.data)

"data": [
  {
      "person": [
          "person1",
          "person2",
          "person3"
      ],
      "_id": "5ca1a9bdf52c0a5710012415",
      "eventName": "special2",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Wed,Tue",
      "__v": 0
  },
  {
      "person": [
          "person1",
          "person2",
          "person3",
          "person2",
          "person3"
      ],
      "_id": "5ca1ae5cd182b35534c89174",
      "eventName": "special1",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Tue,Sat",
      "__v": 0
  }
]

当我调试时,结果是这样的

enter image description here

但未经调试,结果是这样的

enter image description here

我想要第一个

请帮助孩子进行质量检查

2 个答案:

答案 0 :(得分:0)

有关在旧版浏览器中支持.flat()的信息,请参见SetFlat@babel/polyfill

const data = [
  {
      "person": [
          "person1",
          "person2",
          "person3"
      ],
      "_id": "5ca1a9bdf52c0a5710012415",
      "eventName": "special2",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Wed,Tue",
      "__v": 0
  },
  {
      "person": [
          "person1",
          "person2",
          "person3",
          "person2",
          "person3"
      ],
      "_id": "5ca1ae5cd182b35534c89174",
      "eventName": "special1",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Tue,Sat",
      "__v": 0
  }
]

const newData = [
  ...new Set(
    data
      .map(d => d.week !== 14 ? d.person : null)
      .filter(Boolean)
      .flat()
  )
]

console.log(newData)

答案 1 :(得分:0)

您可以使用简单的Set来获取唯一的项,然后展平数组:

const sDate = {
  "data": [{
      "person": [
        "person1",
        "person2",
        "person3"
      ],
      "_id": "5ca1a9bdf52c0a5710012415",
      "eventName": "special2",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Wed,Tue",
      "__v": 0
    },
    {
      "person": [
        "person1",
        "person2",
        "person3",
        "person2",
        "person3"
      ],
      "_id": "5ca1ae5cd182b35534c89174",
      "eventName": "special1",
      "eventNote": "没有留言呦",
      "eventType": "special",
      "week": 13,
      "time": "Sun,Mon,Tue,Sat",
      "__v": 0
    }
  ]
};

let needGenerateWeek = [...new Set(sDate.data.flatMap(({ person }) => person))];
console.log(needGenerateWeek);