1

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];
    }     
}
6
  • Reverse for loop and splice when condition satisfies. Commented May 24, 2017 at 5:11
  • Is the date format m/d/yyyy? Commented May 24, 2017 at 5:12
  • yes m/d/yyyy @Tushar Commented May 24, 2017 at 5:12
  • i tired splice not woking correctly i think so @Tushar Commented May 24, 2017 at 5:13
  • kindly recorrect my code @Jaromanda Commented May 24, 2017 at 5:19

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Stephen L no filter i need to remove that object no need filter
my need is i have one textbox when i click number i need to that particular month from array
2

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

Comments

2

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

4 Comments

Not relevant to question. OP wants to remove objects from the array whose index is not known beforehand. Also, OP is aware of how splice works.
I don't think he does. See MainStateData.splice ([i]); above.
i just try that way @Stephen L
Try Data.splice(i, 1) as @azad mentioned in one of the answers. Beware though, if you click multiple months you can't get them back!
1

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

2 Comments

It should be [0], the format of date is m/d/yyyy
in the data object its look like dd/mm/yyyy
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.