【问题标题】:Filter an array by value with another array of values使用另一个值数组按值过滤数组
【发布时间】:2020-06-24 08:52:31
【问题描述】:

这里是第一个数组“filteredPrestations”的示例元素:

​​[
  2: {
   name: 'Test',
   service: {
     _id:'12345678910'
   }
  }
]

这是我的 ID 数组“serviceIds”:

[
  1: "12345678910"
  2: "10987654321"
  3: "13579246790"
]

我想过滤第一个数组,只使用它们在第二个数组的列表中具有服务 ID 的元素。

我试过了:

this.filteredPrestations.filter(item => item.service._id.includes(this.servicesIds));

谢谢!

【问题讨论】:

  • serviceIds的语法无效,声明时会报错
  • ^^ 作为第一个数组 ...
  • @CertainPerformance 我认为他试图转录它在控制台中的样子,而不是源代码语法。

标签: javascript arrays vue.js filter


【解决方案1】:

您的.includes() 呼叫是向后的。应该是

this.serviceIds.includes(item.service._id)

一般格式为array.includes(element)

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多