Removing items from a javascript array - possibly using JavaScript's delete function? [duplicate]

StackOverflow https://stackoverflow.com/questions/22552507

  •  18-06-2023
  •  | 
  •  

Domanda

I'm using Flot JS charts. I'm trying to remove one of the plot series from my data array completely with jquery or plain javascript.

This is what my data array looks like:

[
 {
  "label" : "Citrix PV Ethernet Adapter",
  "data": [[1,0], [2,0.01], [3,0.02], [4,0.01], [5,0.01]]
 },
 {
  "label" : "Virtual Adapter _1",
  "data" : [[1,0], [2,0], [3,0], [4,0], [5,0]]
 }
]

I want to be able to have the ability to completely remove one of the entries, preferably by using the "label" properties value, so it looks like this:

[
 {
  "label" : "Citrix PV Ethernet Adapter",
  "data": [[1,0], [2,0.01], [3,0.02], [4,0.01], [5,0.01]]
 }
]

Is this possible? I know it's possible to "toggle" the visibility of the series in Flot, but this doesn't suit in this case.

There could be many many entries in this array, I've just kept it to two for an example.

I've tried the playing around with javascripts delete function but I've had no luck so far.

How can this be done?

È stato utile?

Soluzione

Assuming you know the index of the element you want to remove, you can then use splice() to remove it.

So given:

var chartDataArray = [
 {
  "label" : "Citrix PV Ethernet Adapter",
  "data": [[1,0], [2,0.01], [3,0.02], [4,0.01], [5,0.01]]
 },
 {
  "label" : "Virtual Adapter _1",
  "data" : [[1,0], [2,0], [3,0], [4,0], [5,0]]
 },
 {
  "label" : "Citrix PV Ethernet Adapter 2",
  "data": [[3,0.02], [4,0.01], [5,0.01]]
 },
 {
  "label" : "Virtual Adapter _3",
  "data" : [[1,0], [2,0], [3,0]]
 }
]

Then following will remove the item at index 2:

chartDataArray.splice(2, 1);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top