将CENTER数组的CENTER常量更改为循环/迭代?

时间:2019-01-07 00:39:10

标签: arrays react-native object constants

我有一个带有const CENTER辅助功能的工作点心。

我正在尝试将const CENTER更改为具有足够坐标的CENTER数组,以便帮助函数可以迭代/循环?通过所有观点。

var url = location.pathname.substr(1), // remove trailing slash
link = document.querySelector('.nav-link[href$="' + url + '"]');

if (link) {
    var item = link.parentNode;
    while (item.className && (item.nodeName === 'UL' || item.nodeName === 'LI')) {
        if (item.className.indexOf('nav-item') > -1) {
            item.className += ' active';
        }
        item = item.parentNode;
    }
}

我想像这样放置一个CENTER数组:它可以工作吗?

    const { latitude, longitude, speed } = position.coords;
    const center = { latitude: 37.600530, longitude: -122.482629 };  //this is to change
    const { radius, distance } = this.calculateMeasurements(latitude, longitude, center);


    calculateMeasurements = (latitude, longitude, center) => {
    const radius = geolib.isPointInCircle(
      { latitude: latitude, longitude: longitude },
      { latitude: center.latitude, longitude: center.longitude },
      1000
    );
    const distance = geolib.getDistance(
      { latitude: latitude, longitude: longitude },
      { latitude: center.latitude, longitude: center.longitude }
    );
    console.log(radius, distance);
    return { radius, distance };
  }

1 个答案:

答案 0 :(得分:0)

它将很好地工作。 使用map如下循环数组。 并且在数组中使用对象元素时不要忘记使用大括号。

const { latitude, longitude, speed } = position.coords;
const centers = [ // use curly brackets for js objects.
{ id: 1, latitude: 34.3434656, longitude: -56.876456 },
{ id: 2, latitude: 35.3434656, longitude: -57.876456 },
{ id: 3, latitude: 36.3434656, longitude: -58.876456 },
{ id: 4, latitude: 37.3434656, longitude: -59.876456 }];

const { radius, distance } = this.calculateMeasurements(latitude, longitude, center);

const results = centers.map(({id, lati: latitude, longi: longitude) => {
  return this.calculateMeasurements(latitude, longitude, {lati, longi});
});

console.log(`results[0] : ${results[0].radius}, ${results[0].distance}`);

calculateMeasurements = (latitude, longitude, center) => {
  const radius = geolib.isPointInCircle(
  { latitude: latitude, longitude: longitude },
  { latitude: center.latitude, longitude: center.longitude },
  1000);
  const distance = geolib.getDistance(
    { latitude: latitude, longitude: longitude },
    { latitude: center.latitude, longitude: center.longitude }
  );
  console.log(radius, distance);
  return { radius, distance };
}