1new Vue({
2 el: '#app',
3 template: `<div> The nearest city to you is: {{minDistance}}
4</div>`,
5 data: {
6 userCoordinate: [1, 2],
7 allCooridates: [
8 { name: 'City1', coodinate: [1, 7] },
9 { name: 'City2', coodinate: [2, 4] },
10 { name: 'City3', coodinate: [3, 5] },
11 { name: 'City4', coodinate: [1, 4] },
12 ],
13 },
14 methods: {
15 calculateDistance(x, y) {
16 return Math.abs(x[0] - y[0]) + Math.abs(x[1] - y[1])
17 },
18 },
19 computed: {
20 minDistance() {
21 let minDist = Infinity
22 let city = null
23 this.allCooridates.forEach((cord) => {
24 let d = this.calculateDistance(cord.coodinate,
25this.userCoordinate)
26 if (d < minDist) {
27 minDist = d
28 city = cord.name
29 }
30 })
31 return city
32 },
33 },
34})