Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript delete JSON elements

I have this JSON string in a PHP page:

{
  "elements": [{
    "type": "pie",
    "alpha": 0.3,
    "animate": [{
      "type": "fade"
    }, {
      "type": "bounce",
      "distance": 5
    }],
    "start-angle": 0,
    "tip": "#val# de #total# #percent#",
    "colours": ["#d01f3c", "#356aa0", "#C79810"],
    "values": [{
      "value": 1,
      "label": "procesador amd sempron 140"
    }, {
      "value": 1,
      "label": "procesador sempron le130"
    }, {
      "value": 1,
      "label": "procesador amd a4-3300 x2"
    }, {
      "value": 1,
      "label": "procesador intel celeron g530"
    }]
  }],
  "title": {
    "text": "Procesadores, Reinicio",
    "style": "color: #356aa0; font-size: 20px"
  },
  "bg_colour": "#FFFFFF",
  "x_axis": null
}

I call it like this:

$.getJSON("restart_proce.php", function(json)
{    
console.log(json);

I need to transform it to this:

[{\"value\": 1, \"label\": \"procesador amd sempron 140\" }, { \"value\": 1, \"label\": \"procesador sempron le130\" }, { \"value\": 1, \"label\": \"procesador amd a4-3300 x2\" }, { \"value\": 1, \"label\": \"procesador intel celeron g530\" } ]

I'm trying to delete elements like this:

delete json.elements[3];

but it doesn't delete anything. How can I make it work?

like image 517
Oscar Avatar asked Jan 25 '26 09:01

Oscar


1 Answers

Removing an item from an Array:

There are several ways. The splice method is the most versatile:

data.items.splice(3, 1); // Removes three items starting with the 2nd,

splice modifies the original array, and returns an array of the items you removed.

like image 86
laxonline Avatar answered Jan 27 '26 23:01

laxonline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!