简体   繁体   中英

how to remove object from array element in javascript

I am using Javascript. I have a set of data I need to remove the object based on element value. Here I have attached my code. In my code I have month element. I need to remove the object when month is 1 . How to do this?

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);

Code I tried:

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

Try Data.filter(obj => obj["DispenseMonth"].split('/')[0] !== '1') The filter method loops through the Data array and calls the function obj => obj["DispenseMonth"].split('/')[0] !== '1' on every object. If the callback returns true, it keeps the item in array.

Data = Data.filter(obj => obj["DispenseMonth"].split('/')[0] !== '1') will effectively delete the object from the array.

First find the index of the element/object that you want to remove.

For example, let's say you want to remove 2nd element(0-indexed)

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); 

Reference to the docs: MDN Array.splice

You can use reverse for loop with splice to remove the items from array.

for (var i = Data.length - 1; i >= 0; i--) {
    if (Data[i].DispenseMonth.split('/')[0] === '1') {
        Data.splice(i, 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" } ]; for (var i = Data.length - 1; i >= 0; i--) { if (Data[i].DispenseMonth.split('/')[0] === '1') { Data.splice(i, 1); } } console.log(Data); 

you can go thorough your array in reverse and check for month

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);
}

Use filter method:

var newArray = Data.filter(function(value){
  return value.DispenseMonth.split('/')[0]!=1;
});

// this will give the array which do not contain objects with month=1
console.log(newArray);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM