3

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?

4
  • 1
    I think you are looking for Array.splice Commented Dec 26, 2012 at 18:05
  • stackoverflow.com/questions/2901562/… Commented Dec 26, 2012 at 18:06
  • 1
    delete will set that array element to undefined, but it will not remove the element. Use splice instead. Ofcourse, in this example there is only 1 element in that elements array, so json.elements[3] is accessing nothing. Commented Dec 26, 2012 at 18:10
  • There only seems to be one element in the array contained in the elements property. Commented Dec 26, 2012 at 18:14

4 Answers 4

2

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.

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

Comments

2

Try this:

json.elements.splice(3, 1);

See: Array.splice

Comments

1

Just modify the values directly before console.log(json)

json= json.elements[0].values

Or in the restart_proce.php php page just echo

echo json_encode($data['elements'][0]['values']); // if associative array is used.

Comments

0

Try this:

var data = {"result":[
  {"FirstName":"Test1","LastName":"User","Email":"[email protected]","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
  {"FirstName":"user","LastName":"user","Email":"[email protected]","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
  {"FirstName":"Ropbert","LastName":"Jones","Email":"[email protected]","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
  {"FirstName":"hitesh","LastName":"prajapti","Email":"[email protected]","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
  ]
}
alert(data.result);
delete data.result[3];
alert(data.result);

working JSFiddle

or You can use splice to remove elements from an array.

1 Comment

Won't work as it sets the element to undefined. There are "freak" situations in which this works though, so it shouldn't be trusted.

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.