Как я могу отфильтровать VueJs? - PullRequest
0 голосов
/ 20 апреля 2020

Я использую vue. js в своем проекте, и мне нужно отфильтровать это json в моем html и отображать только то, что с areas = area_one, в настоящее время я использую один фильтр, но он отображает все элементы моего json

JSON

   {
      "ID":789,
      "title":"Title Page",
      "image_desktop":"image21.jpg",
      "image_mobile":"image234.jpg",
      "link":"#",
      "areas":[
         "area_one"
      ]
   },
   {
      "ID":765,
      "title":"Title Page 2",
      "image_desktop":"image231.jpg",
      "image_mobile":"image421.jpg",
      "link":"#\/link",
      "areas":[
         "area_two"
      ]
   }
]

и vue HTML

   <div v-for="example in filtered_examples" :key="example.ID">
      <img :src="example.image_mobile">
      <img :src="example.image_desktop">
    </div>   

Мне нужно отфильтровать только один из области в моем html

Filter_example:


  this.filtered_examples = !this.area ? this.examples : this.examples.filter(b => b.areas.indexOf(this.area) > -1 )

1 Ответ

0 голосов
/ 21 апреля 2020

Это отлично работает.

var examples = [{
    "ID": 789,
    "title": "Title Page",
    "image_desktop": "image21.jpg",
    "image_mobile": "image234.jpg",
    "link": "#",
    "areas": [
      "area_one"
    ]
  },
  {
    "ID": 765,
    "title": "Title Page 2",
    "image_desktop": "image231.jpg",
    "image_mobile": "image421.jpg",
    "link": "#\/link",
    "areas": [
      "area_two"
    ]
  }
];
var area = 'area_one';
var filtered_examples = examples.filter(b => b.areas.indexOf(area) > -1);
console.log(filtered_examples);
...