如何在javascript中从数组元素中删除对象

时间:2017-05-24 05:10:41

标签: javascript arrays

我正在使用Javascript。我有一组数据需要根据元素值删除对象。在这里,我附上了我的代码。在我的代码中,我有月份元素。我需要在月份1时删除该对象。怎么做?

var Data=[ 
    { "State": "PA", "DispenseMonth": "1/1/2017" }, 
    { "State": "MS", "DispenseMonth": "1/1/2017" }, 
    { "State": "CT", "DispenseMonth": "1/1/2017" }, 
    { "State": "TX", "DispenseMonth": "2/1/2017"}, 
    { "State": "DE", "DispenseMonth": "2/1/2017"}, 
    { "State": "TN", "DispenseMonth": "2/1/2017" }, 
    { "State": "FL", "DispenseMonth": "3/1/2017" }, 
    { "State": "SD", "DispenseMonth": "4/1/2017" }, 
    { "State": "GA", "DispenseMonth": "5/1/2017"}, 
    { "State": "SC", "DispenseMonth": "6/1/2017"}, 
    { "State": "IA", "DispenseMonth": "7/1/2017" }, 
    { "State": "RI", "DispenseMonth": "8/1/2017" }, 
    { "State": "ID", "DispenseMonth": "9/1/2017"}
] 

Data.forEach(item => {
     return item.Month = item.DispenseMonth.split('/')[0];
});
console.log(Data);

我试过的代码:

 for(i = 0; i < MainStateData.length; i++) {
    var bjMonth = MainStateData[i].Month;
    if (bjMonth == 1) {
        delete MainStateData[bjMonth]; 
        MainStateData.splice([i]);
        delete MainStateData[i];
    }     
}

5 个答案:

答案 0 :(得分:2)

试试Data.filter(obj => obj["DispenseMonth"].split('/')[0] !== '1') filter方法遍历Data数组并在每个对象上调用函数obj => obj["DispenseMonth"].split('/')[0] !== '1'。如果回调返回true,则将项目保留在数组中。

Data = Data.filter(obj => obj["DispenseMonth"].split('/')[0] !== '1')将有效地从数组中删除该对象。

答案 1 :(得分:2)

首先找到要删除的元素/对象的索引。

例如,假设你要删除第二个元素(0索引)

var array = ["Apple", "Mango", "Grapes", "Bread"];
var grape = array.splice(2, 1);

var Data = [{
    "State": "PA",
    "DispenseMonth": "1/1/2017"
  },
  {
    "State": "MS",
    "DispenseMonth": "1/1/2017"
  },
  {
    "State": "CT",
    "DispenseMonth": "1/1/2017"
  },
  {
    "State": "TX",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "DE",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "TN",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "FL",
    "DispenseMonth": "3/1/2017"
  },
  {
    "State": "SD",
    "DispenseMonth": "4/1/2017"
  },
  {
    "State": "GA",
    "DispenseMonth": "5/1/2017"
  },
  {
    "State": "SC",
    "DispenseMonth": "6/1/2017"
  },
  {
    "State": "IA",
    "DispenseMonth": "7/1/2017"
  },
  {
    "State": "RI",
    "DispenseMonth": "8/1/2017"
  },
  {
    "State": "ID",
    "DispenseMonth": "9/1/2017"
  }
];

Data.forEach(function(item) {
  return item.Month = parseInt(item.DispenseMonth.split('/')[0], 10);
});

console.log("Array Length Before Deletion: ", Data.length);
// Option 1: Both works
 for (i =  Data.length - 1; i >= 0; --i) {
  var bjMonth = Data[i].Month;
  if (bjMonth === 1) {
    console.log("Deleted: ", Data[i].State);
    Data.splice(i, 1);
  }
}
// Option 2: Both works
/*Data = Data.filter(function(item, index) {
  return item.Month > 1
});*/
console.log("Array Length After Deletion: ", Data.length);

参考文档:MDN Array.splice

答案 2 :(得分:2)

您可以对for使用反向splice循环从数组中删除项目。

for (var i = Data.length - 1; i >= 0; i--) {
    if (Data[i].DispenseMonth.split('/')[0] === '1') {
        Data.splice(i, 1);
    }
}

&#13;
&#13;
var Data = [{
    "State": "PA",
    "DispenseMonth": "1/1/2017"
  }, {
    "State": "MS",
    "DispenseMonth": "1/1/2017"
  }, {
    "State": "CT",
    "DispenseMonth": "1/1/2017"
  },
  {
    "State": "TX",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "DE",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "TN",
    "DispenseMonth": "2/1/2017"
  },
  {
    "State": "FL",
    "DispenseMonth": "3/1/2017"
  },
  {
    "State": "SD",
    "DispenseMonth": "4/1/2017"
  },
  {
    "State": "GA",
    "DispenseMonth": "5/1/2017"
  },
  {
    "State": "SC",
    "DispenseMonth": "6/1/2017"
  },
  {
    "State": "IA",
    "DispenseMonth": "7/1/2017"
  },
  {
    "State": "RI",
    "DispenseMonth": "8/1/2017"
  },
  {
    "State": "ID",
    "DispenseMonth": "9/1/2017"
  }
];

for (var i = Data.length - 1; i >= 0; i--) {
  if (Data[i].DispenseMonth.split('/')[0] === '1') {
    Data.splice(i, 1);
  }
}
console.log(Data);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

你可以反过来彻底检查你的阵列并查看月份

var month = '1';

for(var i = Data.length - 1; i > -1 ; i-- ){
   //date format dd/m/yyyy             
   if(Data[i].DispenseMonth.split('/')[1] == month)
       Date.splice(i, 1);
}

答案 4 :(得分:1)

使用filter方法:

{{1}}

希望这有帮助。