gap
V2EX  ›  Vue.js

请问在 vue 2 中如何能在 v-for 对象情况下根据对象某个 key 来排序列表呢?

  •  
  •   gap · May 22, 2017 · 4467 views
    This topic created in 3282 days ago, the information mentioned may be changed or developed.

    由于数据结构的原因,v-for 的是对象属性,每个属性又是一个对象,现在希望让列表按 order 这个属性进行排序,请问能做到吗?

    const objects = {
      prop1: { text: 'a', order: 3 },
      prop2: { text: 'c', order: 1 },
      prop3: { text: 'b', order: 2 },
    }
    <template v-for="(value, key) in objects">
      <div>{{ value.text }}</div>
    </template>
    

    希望的 output

    c
    b
    a
    
    4 replies    2017-06-02 13:52:48 +08:00
    Biwood
        2
    Biwood  
       May 22, 2017
    手动给数据排序也不是什么麻烦事,新建一个数组,用 order 作为索引并赋值对应的值
    Sparetire
        3
    Sparetire  
       May 23, 2017 via Android
    用计算属性吧
    470326964
        4
    470326964  
       Jun 2, 2017
    计算属性返回值,然后拿返回值去遍历。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2955 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 15:22 · PVG 23:22 · LAX 08:22 · JFK 11:22
    ♥ Do have faith in what you're doing.