admin管理员组

文章数量:1392110

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

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];
    }     
}
Share Improve this question edited May 24, 2017 at 5:21 Web_Designer 74.8k93 gold badges210 silver badges267 bronze badges asked May 24, 2017 at 5:10 josejose 1,0541 gold badge12 silver badges36 bronze badges 6
  • Reverse for loop and splice when condition satisfies. – Tushar Commented May 24, 2017 at 5:11
  • Is the date format m/d/yyyy? – Tushar Commented May 24, 2017 at 5:12
  • yes m/d/yyyy @Tushar – jose Commented May 24, 2017 at 5:12
  • i tired splice not woking correctly i think so @Tushar – jose Commented May 24, 2017 at 5:13
  • kindly recorrect my code @Jaromanda – jose Commented May 24, 2017 at 5:19
 |  Show 1 more ment

5 Answers 5

Reset to default 2

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.

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

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 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.

本文标签: how to remove object from array element in javascriptStack Overflow