I have a json object:
var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');
so, I want to add/remove data from this object by jquery. How to do it? can I convert it to Array? Thanks
Below will give you a javascript object,
var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');
You can then use .push & .pop to add/remove element into the array.
deniedTimeIDs.push(100); //will result in [808,809,812,811,814,815,100]
Further Readings,
JSON.parse, Array.push, Array.pop
If you want to parse that String and represent it as an Array you can do the following:
// Warning: eval is weird
var arr = eval('[808,809,812,811,814,815]');
or
var arr= JSON.parse('[808,809,812,811,814,815]');
Now arr
is a valid JavaScript array.
UPDATE FROM 2021 ADDING AN OFFICIAL DOC ARTICLE WHICH EXPLAINS WHY eval()
IS A DANGEROUS FUNCTION TO CALL:
eval()
Any Array returned after parsing the String, can be processed with jQuery or JavaScript. We generally use Push() and Pop() function to process any array.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
var deniedTimeIDs = JSON.parse('[808,809,812,811,814,815]');
// You can use push/Pop to remove the IDs from an array.
console.log(deniedTimeIDs);// o/p=> [808,809,812,811,814,815]
//You can iterate this array using jQuery.
$.each(deniedTimeIDs,function(key,val){
console.log(val);
})
});
</script>
var deniedTimeIDs = $.parseJSON('[808,809,812,811,814,815]');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With