【问题标题】:VueJs - Problem with alphabetic sorting in ArrayListVueJs - ArrayList 中的字母排序问题
【发布时间】:2021-01-09 14:46:07
【问题描述】:

我从 VueJs 开始,但在 ArrayList 中遇到了字母排序问题。

我想用艺术家姓名按字母顺序排列我的卡片。

我搜索了有关此的信息,但没有找到答案。

你能给我定位吗?我被锁了。

这是我的代码:

HTML

<div class="container-fluid d-flex flex-wrap">
      <div
        class="card_prog col-md-6 col-xl-4"
        v-for="(card, index) in filteredCategory"
        :key="index"
      >
        <div class="card_title">
          <h3>{{ card.artist }}</h3>
          <span> {{ card.stage }} / {{ card.day }} / {{ card.hour }} </span>
        </div>
        <div class="container_img">
          <img class="card_img" :src="card.img" />
        </div>
      </div>
    </div>
  </div>

JS

let types = [
  "rap",
  "rock",
  "metal",
  "pop",
  "electro",
  "folk",
  "jazz",
  "alternatif",
];
let artist = [
  "Booba",
  "Vald",
  "Petit Biscuit",
  "Marlin Manson",
  "Céline Dion",
  "Jean Schultheis",
  "Psy",
  "Cardi B",
  "Scorpion",
  "Fatal Bazooka",
  "Tragédie",
  "Keen v",
];
let stage = ["Scène A", "Scène B", "Scène C"];
let hour = ["18h", "19h", "20h", "21h", "22h", "23h", "00h", "01h00"];
let day = ["Vendredi", "Samedi", "Dimanche"];
const cardsData = [
  {
    artist: artist[2],
    type: types[0],
    img:
      "https://media.virginradio.fr/article-4260166-head-f8/petit-biscuit.jpg",
    stage: stage[0],
    day: day[2],
    hour: hour[4],
  },
  {
    artist: artist[0],
    type: types[1],
    img:
      "https://www.parisladefense-arena.com/uploads/2018/10/3764-booba-orig-2.jpg",
    stage: stage[1],
    day: day[0],
    hour: hour[5],
  },
  {
    artist: artist[1],
    type: types[2],
    img:
      "https://cdn.radiofrance.fr/s3/cruiser-production/2019/04/05e9523a-428f-4798-ad07-c4abcc70acfa/801x410_vald_album_2019.jpg",
    stage: stage[2],
    day: day[1],
    hour: hour[5],
  },
  {
    artist: artist[5],
    type: types[0],
    img:
      "https://static1.purepeople.com/articles/6/30/54/86/@/4322687-semi-exclusif-jean-schultheis-vendre-950x0-1.jpg",
    stage: stage[0],
    day: day[1],
    hour: hour[6],
  },
];

**VUE JS **

export default {
  data() {
    return {
      cards: cardsData,
      types: types,
      days: day,
      hours: hour,
      stages: stage,
      selectedType: "All",
    };
  },
  computed: {
    filteredCategory: function () {
      let self = this;
      let cardsArray = self.cards;
      let selectedType = self.selectedType;
      if (selectedType === "All") {
        return cardsArray
      } else {
        cardsArray = cardsArray.filter(function (card) {
          if (
            card.type.indexOf(selectedType) !== -1 ||
            card.day.indexOf(selectedType) !== -1 ||
            card.hour.indexOf(selectedType) !== -1 ||
            card.stage.indexOf(selectedType) !== -1
          ) {
            return card;
          }
        });
      }
      return cardsArray;
    },
  },
};
</script>

提前,非常感谢你:)

【问题讨论】:

  • 为什么将this 重命名为self btw ?它增加了更多的复杂性。

标签: vue.js vue-cli


【解决方案1】:

编辑: 好吧,用一个更干净的对象

const items = [
  {
    artist: "Petit Biscuit",
    type: "rap",
    img:
      "https://media.virginradio.fr/article-4260166-head-f8/petit-biscuit.jpg",
    stage: "Scène A",
    day: "Samedi",
    hour: "22h",
  },
  {
    artist: "Booba",
    type: "rock",
    img:
      "https://www.parisladefense-arena.com/uploads/2018/10/3764-booba-orig-2.jpg",
    stage: "Scène B",
    day: "Vendredi",
    hour: "23h",
  },
  {
    artist: "Vald",
    type: "metal",
    img:
      "https://cdn.radiofrance.fr/s3/cruiser-production/2019/04/05e9523a-428f-4798-ad07-c4abcc70acfa/801x410_vald_album_2019.jpg",
    stage: "Scène C",
    day: "Samedi",
    hour: "23h",
  },
  {
    artist: "Jean Schultheis",
    type: "rap",
    img:
      "https://static1.purepeople.com/articles/6/30/54/86/@/4322687-semi-exclusif-jean-schultheis-vendre-950x0-1.jpg",
    stage: "Scène A",
    day: "Samedi",
    hour: "00h",
  },
]

items.sort((a, b) => a.artist.localeCompare(b.artist))

这个得到了他们的正确订购:Booba、Jean Schultheis、Petit biscuit,最后是 Vald。


这个应该可以的。

let items = ['réservé', 'Premier', 'Cliché', 'communiqué', 'café', 'Adieu'];
items.sort( (a, b) => a.localeCompare(b, 'fr', {ignorePunctuation: true}));
// ['Adieu', 'café', 'Cliché', 'communiqué', 'Premier', 'réservé'] 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#sort_an_array

【讨论】:

  • 感谢@kissu 的建议。我不能应用你的方法。我不知道该怎么做。当用户检查我的页面“程序”时,我想显示排序字母艺术家卡片。
  • 。 ' 计算:{过滤类别:函数(){让自我=这个;让cardArray = self.cards;让 selectedType = self.selectedType; if (selectedType === "All") { return cardsArray }'
  • 我可以帮你写这个。你能用真实值更新你的cardsData吗?而不是硬编码对许多其他变量的引用。为什么不只有一个对象数组?
猜你喜欢
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 2013-05-09
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多