js如何删除json里的值
发布网友
发布时间:2022-05-09 18:22
我来回答
共2个回答
热心网友
时间:2022-04-22 20:18
思路
第一种方法:通过把json中需要的值取出来,重新生成json对象,这种方法比较笨
第二种方法:通过delete 删除属性,这种方法比较常用,在第三方js库中经常能看到,推荐
举例
<script>
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>
热心网友
时间:2022-04-22 21:36
<!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>