json 添加 和刪除兩種方法


<script>
var test = { name: "name", age: "12" };
var countrys = {
"newval": [{ "Country_code": "101", "Country_name": "中國" },
{ "Country_code": "102", "Country_name": "美國" }]
};
$(function () {
test.id = "1245";
alert(test.id);

//添加
var c = { "Country_code": "103", "Country_name": "英國" };
countrys.newval.push(c)
alert(countrys.newval[0].Country_name)
//刪除
delete countrys.newval[1];
alert(countrys.newval[0].Country_name)
})
</script>

 

var str1 = {"name": "apple", "sex": "21"};

// 參數:prop = 屬性,val = 值
function createJson(prop, val) {
// 如果 val 被忽略
if(typeof val === "undefined") {
// 刪除屬性
delete str1[prop];
}
else {
// 添加 或 修改
str1[prop] = val;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM