js如何删除json里的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js如何删除json里的值相关的知识,希望对你有一定的参考价值。
提问:var s = ["name":"tank","total":"100","name":"zhang","total":"23","name":"hao","total":"325"]
怎么把“name”去掉 变成 test1 = ["total":"100","total":"23","total":"325"]json串
思路
第一种方法:通过把json中需要的值取出来,重新生成json对象,这种方法比较笨
第二种方法:通过delete 删除属性,这种方法比较常用,在第三方js库中经常能看到,推荐
举例
var s = ["name":"tank","total":"100","name":"zhang","total":"23","name":"hao","total":"325"];
for(var k=0;k<s.length;k++)
delete s[k]["name"];
</script> 参考技术A <!DOCTYPE html>
<html>
<head>
<title>yugi</title>
<meta charset=UTF-8 />
<style type="text/css">
</style>
<script type="text/javascript">
var s = [
"name" : "tank",
"total" : "100"
,
"name" : "zhang",
"total" : "23"
,
"name" : "hao",
"total" : "325"
];
for ( var i = 0; i < s.length; i++)
delete s[i].name;
console.log(s);
</script>
</head>
<body>
</body>
</html>本回答被提问者和网友采纳
以上是关于js如何删除json里的值的主要内容,如果未能解决你的问题,请参考以下文章