简体   繁体   English

在 vuejs 中过滤 const 数据

[英]filtering const data in vuejs

i have a vue app with ad group of data.我有一个带有广告组数据的 vue 应用程序。

const data = [
 {
   name: 'value1',
   id: 'id1',
   supervisor: true,
 },
 {
  name: 'value2'
  id: 'id2',
  supervisor: false,
 }
]

but i just need to display data for supervisor = true only.但我只需要为 supervisor = true 显示数据。

i'd tried to filter it with belows code but it did not work.我试图用下面的代码过滤它,但它没有用。

const test = data.filter(data => data.supervisor = true);
console.log(test);

can somenone share any reference for me to refer for this filtering function as i new in vue js.有人可以为我分享任何参考资料,以参考我在 vue js 中的新功能。 or anyone can share their knowledge.或者任何人都可以分享他们的知识。 because actually i have the data from api which need to be display with some sort of condition.因为实际上我有来自 api 的数据,这些数据需要在某种条件下显示。

i have tried using url/api/items?supervisor=true and then i was inform that the API did no support for that kind of way so i need to create a filter function in javascript from data that retrieve from APIs.我尝试使用url/api/items?supervisor=true ,然后我被告知 API 不支持这种方式,所以我需要在 ZDE9B9ED78D7E2E1DCEEFFEE780E2F 中检索数据的 ZDE9B9ED78D7E2E1DCEEFFEE780E2F 中创建一个过滤器 function。 i'm using above const data =[{...,...}] in this question for it easier to understand.我在这个问题中使用了上面的const data =[{...,...}]以便更容易理解。

You should use const test = data.filter(data => data.supervisor);你应该使用const test = data.filter(data => data.supervisor); instead of > true而不是> true

const test = data.filter(data => data.supervisor === true);

Full working example, since your data was also missing a comma, so the not working part can be from there:完整的工作示例,因为您的data也缺少逗号,所以不工作的部分可以来自那里:

const data = [
 {
   name: 'value1',
   id: 'id1',
   supervisor: true,
 },
 {
  name: 'value2',
  id: 'id2',
  supervisor: false,
 }
];
const test = data.filter(data => data.supervisor === true);
console.log(test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM