如何使用对象过滤对象数组

我想通过另一个数组的项目过滤一个对象数组。这是我的物品清单:


const list = ["first", "second", "third"]

这个数组可以是空的,有一个,两个或三个 itens...例如:


const list = []

const list = ["first"]

let list = ["first", "third"]

所以,我需要使用我的状态list来过滤我myArr2的。


const myArr2 = [

  {

    id: "1",

    language: "portuguese",

    status: "first"    

  },

  {

    id: "2",

    language: "portuguese",

    status: "first"    

  },

  {

    id: "3",

    language: "portuguese",

    status: "second"    

  },

  {

    id: "4",

    language: "portuguese",

    status: "third"    

  },      

  {

    id: "5",

    language: "portuguese",

    status: "second"    

  },      

]

如果我list的是:


const list = [ "first", "third" ]

输出必须是:


const myArr2 = [

  {

    id: "1",

    language: "portuguese",

    status: "first"    

  },

  {

    id: "2",

    language: "portuguese",

    status: "first"    

  },

  {

    id: "4",

    language: "portuguese",

    status: "third"    

  },      

]

如果我list的是:


const list = [ "first" ]

输出必须是:


const myArr2 = [

  {

    id: "1"

    language: "portuguese"

    status: "first"    

  },

  {

    id: "2"

    language: "portuguese"

    status: "first"    

  }  

}

等等...


我制作了这段代码但无法正常工作:


Object.entries(list).forEach(status => {

  myArr2.filter(item => {

    if(item.status === status){

      return item

    }

  })

})

我究竟做错了什么?有人可以帮助我吗?


慕雪6442864
浏览 81回答 3
3回答

陪伴而非守候

您正在以我将要说的相反方式进行操作。您需要检查 myArr2 中的每个项目是否具有列表数组中存在的状态。你可以这样做 -正确的代码 -let myArr2 = [  {    id: "1",    language: "portuguese",    status: "first"      },  {    id: "2",    language: "portuguese",    status: "first"      },  {    id: "3",    language: "portuguese",    status: "second"      },  {    id: "4",    language: "portuguese",    status: "third"      },        {    id: "5",    language: "portuguese",    status: "second"      },      ];const list = [ "first", "third" ]myArr2 = myArr2.filter(item => list.includes(item.status))console.log(myArr2)你的方法有误Object.entries(list).forEach(status => {  myArr2.filter(item => {    if(item.status === status){      return item    }  })})在上述方法中,您将遍历列表中的第一个元素(即first)。因此,它将过滤掉所有myArr2状态不为第一的项目。在下一次迭代中,您将检查列表中的第二个元素(即second)。所以,现在它会进一步过滤掉所有myArr2状态不为的项目second。由于没有元素同时具有 first 和 second 的状态,因此您的代码可能会将结果数组作为空数组返回。注意:您需要将返回的新数组分配myArr2给某个变量。该filter方法不会就地过滤掉元素。因此,由于您还没有这样做,如果您尝试通过控制台日志记录来检查它,您的代码可能会为您提供原始数组本身。filter() 您可以在此处阅读更多信息。

繁花不似锦

你可以过滤数组。如果元素是您的过滤器列表的一部分(indexOf != -1),请检查此项。function filterArray(arr, list) {    return arr.filter(el => {        return list.indexOf(el.status)!=-1;    });}const myArr2 = [  {    id: "1",    language: "portuguese",    status: "first"      },  {    id: "2",    language: "portuguese",    status: "first"      },  {    id: "3",    language: "portuguese",    status: "second"      },  {    id: "4",    language: "portuguese",    status: "third"      },        {    id: "5",    language: "portuguese",    status: "second"      },      ]list1 = []list2 = ["first"]list3 = ["first", "third"]console.log(filterArray(myArr2, list3));console.log(filterArray(myArr2, list2));console.log(filterArray(myArr2, list1));

慕姐4208626

您可以解构status并检查是否list包含此值。result = myArr2.filter(({ status }) => list.includes(status));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript