目标数组随着原始数组排序

`<template>
<div>

测试5

</div>
</template>

<script>
export default {
name: 'Test5',
components: {},
data() {

return {

}

},
computed: {},
created() {},
mounted() {},
methods: {

// 让数组随着另外一个数组排序顺序而排序
arrSortFollow1() {
  const current = [2, 4, 8, 1, 3]
  const target = [
    { name: 'kkw', id: 1 },
    { name: 'qqq', id: 3 },
    { name: 'www', id: 2 },
    { name: 'eee', id: 8 },
    { name: 'rrr', id: 4 }
  ]
  target.sort((a, b) => {
    const m = current.indexOf(a.id)
    const n = current.indexOf(b.id)
    return m - n
  })
},
// 函数抽离 attribute目标属性名,current目标数组
arrSortFollow2(attribute, current) {
  return (a, b) => {
    const m = current.index(a.attribute)
    const n = current.index(b.attribute)
    return m - n
  }
},
// 使用
tempDome() {
  const current1 = [1, 3, 4, 5, 2]
  const current2 = [2, 1, 5, 4, 3]
  const target = [
    { name: 'kkw', id: 1, age: 3 },
    { name: 'qqq', id: 2, age: 4 },
    { name: 'www', id: 3, age: 1 },
    { name: 'eee', id: 4, age: 5 },
    { name: 'rrr', id: 5, age: 2 }
  ]
  target.sort(this.arrSortFollow2('id', current1))
  target.sort(this.arrSortFollow2('age', current2))
}

}
}
</script>

<style scoped lang="scss">

</style>
`

评论

暂无评论