亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將計算值推送到多維 JSON 對象并對父對象和子對象進行排序 Vue.JS

如何將計算值推送到多維 JSON 對象并對父對象和子對象進行排序 Vue.JS

海綿寶寶撒 2021-06-01 14:43:22
我有一個單獨事件的多維 JSON 提要,每個事件都有許多不同的日期和地點。所述事件中的每個日期都包括我使用 HTML5 地理定位計算距離的緯度和經度。我想將該距離推入子對象,不僅按距離排序,還按每個日期的距離對事件進行分組。我曾嘗試使用 v-for 進行內聯排序,但后來我了解到這在 vue2 中不起作用,也不能解決對父事件進行排序的問題。我在下面包含了我正在處理的示例:HTML:<div id="string">  <p><strong>Current Geolocation:</strong> {{lat}}:{{lon}}</p>  <ol v-for="seminar in seminars">    <li>      {{seminar.title}}      <ul>        <li v-for="event in seminar.events">          {{event.webtitle}} <strong>{{calcDist(lat,lon,event.location.lat,event.location.lon,N)}} Miles Away</strong>        </li>      </ul>    </li>  </ol></div>方法:methods: {    getLocation: function () {            if(navigator.geolocation){        navigator.geolocation.getCurrentPosition(this.showPosition, this.errorCallback);      } else {        this.error = "Geolocation is not supported.";      }    },    showPosition: function (position) {       this.lat = position.coords.latitude;      this.lon = position.coords.longitude;      this.googleQuery(position.coords.latitude, position.coords.longitude);    },    calcDist: function (lat1, lon1, lat2, lon2, unit) {      if ((lat1 == lat2) && (lon1 == lon2)) {        return 0;      } else {        var radlat1 = Math.PI * lat1/180;        var radlat2 = Math.PI * lat2/180;        var theta = lon1-lon2;        var radtheta = Math.PI * theta/180;        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);        if (dist > 1) {          dist = 1;        }        dist = Math.acos(dist);        dist = dist * 180/Math.PI;        dist = dist * 60 * 1.1515;        if (unit=="K") { dist = dist * 1.609344 }        if (unit=="N") { dist = dist * 0.8684 }        return Math.round(dist);      }    }  },  beforeMount() {    this.getLocation();  }
查看完整描述

1 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

這似乎是計算屬性的完美用例。我不太明白問題的一些細節(例如,研討會的排序依據是什么?)但下面的一般方法應該可行。為清楚起見,它使用了多個計算屬性,但如果需要,可以將它們組合起來。


computed: {

    seminarsWithDistance() {

        return this.seminars.forEach(seminar => {

            seminar.events.forEach(event => {

                event.distance = calcDist(/*...*/);

            });

        });

    },

    seminarsWithSortedEvents() {

        return this.seminarsWithDistance.forEach(seminar => {

            seminar.events.sort((a, b) => a.distance - b.distance);

        });

    },

    sortedSeminars() {

        return this.seminarsWithSortedEvents.sort((a, b) => {

            /* some compare function for two seminars a and b */

        });

    }

}

那么這只是在模板中使用計算屬性的問題


<ol v-for="seminar in sortedSeminars">


查看完整回答
反對 回復 2021-06-03
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號