从JSON数组中删除行将留下'null'
deleting row from JSON array leaves 'null'
http://jsfiddle.net/J2KuY/
在test2中,您可以看到它不是从数组中删除节点,而是用"null"替换节点。
我做错了什么,如何才能完全消除它?
编辑:使用拼接而不是删除。此处更新小提琴:http://jsfiddle.net/J2KuY/1/
使用拼接:
http://www.w3schools.com/jsref/jsref_splice.asp
这是一个带有链接的示例
//say you have these arrays
var test1 = [{
"Country": "Spain",
"info info1": 0.329235716,
"info info2": 0.447683684,
"info info3": 0.447683747},
{
"Country": "Chile",
"info info1": 1.302673893,
"info info2": 1.357820775,
"info info3": 1.35626442},
{
"Country": "USA",
"info info1": 7.78805016,
"info info2": 26.59681951,
"info info3": 9.200900779}];
var test2 = [{
"Country": "Germany",
"info info1": 0.329235716,
"info info2": 0.447683684,
"info info3": 0.447683747},
{
"Country": "China",
"info info1": 1.302673893,
"info info2": 1.357820775,
"info info3": 1.35626442},
{
"Country": "France",
"info info1": 7.78805016,
"info info2": 26.59681951,
"info info3": 9.200900779}];
//similar to jQuery, wrap them in an object
function $W(param) {
var obj = {};
//set data
obj.data = param;
//augment the object with a remove function
obj.remove = function(key, val) {
var i = 0;
//loop through data
while (this.data[i]) {
if (this.data[i][key] === val) {
//if we have that data, splice it
//splice changes the array length so we don't increment
this.data.splice(i, 1);
} else {
//else move on to the next item
i++;
}
}
//be sure to return the object so that the chain continues
return this;
}
//return object for operation
return obj
}
//the following will look strangely similar to jQuery
//remove chile, then USA
var result1 = $W(test1).remove('Country', 'Chile').remove('Country', 'USA');
//remove germany and china
var result2 = $W(test2).remove('Country', 'China').remove('Country', 'Germany');
//should only have spain and france
$('#test2').val(JSON.stringify(result1)+JSON.stringify(result2));
相关文章:
- AJAX请求中的JSON对象为Null或空
- 从aspx.cs页面获取jquery post中的Null响应而不是json
- D3 Json请求获取XmlHttpRequest错误:访问控制允许原点不允许原点为null
- 为什么change函数内部的全局json对象为null
- Html.DropDownList在JSON中使用.val()作为null传递
- 其中一个json.dumps值为null
- 如何删除“;NULL”;使用Javascript从sails.js中的JSON对象中提取字符串
- Node json-schema prevent null for interger
- 成功填充 xmlhttprequest 的 Json 对象突然变为“null”
- 当 JSON.stringify 时,ASP.NET MVC 控制器参数的 JSON 为 null
- 尝试将使用 SQL 语句生成的 JSON 数据从 PHP 脚本返回到 JS 网页,但改为 null
- JSON 电子表格返回 null,或者我是个白痴
- 序列化的 JSON 对象在控制器处显示为 Null
- 某些 JSON 字段在命中 MVC 控制器方法时显示为 NULL
- 将 null 替换为 JSON 中的引号
- 从JSON数组中删除行将留下'null'
- 当我用JQueryAJAX传递JSON数组时,我会得到null
- 向WCF Rest Service发送JSON-对象文件为null
- 计算机上的JSON文件Access Control Allow Origin不允许Origin为null
- null JSON in servlet