Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从对象数组中删除当前索引_Javascript - Fatal编程技术网

Javascript 从对象数组中删除当前索引

Javascript 从对象数组中删除当前索引,javascript,Javascript,我正在尝试删除与if语句匹配的对象 我的阵列: [ TextRow { id: 4, user_gender: 'Male', user_tags: '[]', user_birth: 26, user_lat: '34.03569255690516', user_lng: '-6.828115666526287', user_current_occupancy: 'None', user_biography:

我正在尝试删除与if语句匹配的对象

我的阵列:

[
   TextRow {
     id: 4,
     user_gender: 'Male',
     user_tags: '[]',
     user_birth: 26,
     user_lat: '34.03569255690516',
     user_lng: '-6.828115666526287',
     user_current_occupancy: 'None',
     user_biography: null,
     set_from_map: 1,
     info_verified: 0
   },
   TextRow {
     id: 5,
     user_gender: 'Male',
     user_relationship: 'Single',
     user_tags: '[]',
     user_birth: 19,
     user_lat: '32.8811',
     user_lng: '-6.9063',
     user_current_occupancy: 'None',
     user_biography: null,
     set_from_map: 1,
     info_verified: 0
   }
 ]
以下是一段代码片段: 在距离函数中,我计算用户和其他用户之间的目的地,我让他们在数据数组中

data.map((value, i) => {

      const destination = parseFloat(
        distance(lat, long, value.user_lat, value.user_lng).toFixed(2)
      );
      // age_range is an array that have the range of age for example[16,26]
      if (
        destination > location_range ||
        value.user_birth < age_range[0] ||
        value.user_birth > age_range[1]
      ) {
        data.splice(i, 1);
        i = i - 1;
      } else value.destination = destination;
    });
它没有达到我想要的效果,提前谢谢你

你可以使用data.filter而不是data.map


选中此项了解更多详细信息

是否可以在筛选函数中计算目的地?是的,您可以通过引用编辑当前项,以便将目的地添加到原始对象中
const data = newData.filter(el => {
const destination = parseFloat(
distance(lat, long, el.user_lat, el.user_lng).toFixed(2)
);
console.log(
destination > location_range ||
el.user_birth < age_range[0] ||
el.user_birth > age_range[1]
);
return !(
destination > location_range ||
el.user_birth < age_range[0] ||
el.user_birth > age_range[1]
);
});
data.forEach(el => {
const destination = parseFloat(
distance(lat, long, el.user_lat, el.user_lng).toFixed(2)
);
el.destination = destination;
});