I want splice the line with value = 3
我要将值= 3的线拼接起来
[3,"John", 90909090]
data.json
data.json
{
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
}
I try this:
我试试这个:
var size = data.rows.length; // number of rows
var del = 3 // Value of ID to be deleted
for (i = 0; i < size; i++) {
var id = data.rows[i][0];
if(del==id){ // if del = id -> splice
data.rows.splice(i,1);
}
}
Results:
结果:
Only splice or only loop this code works.
只有splice或仅循环此代码工作。
But, with both show this error:
但是,两者都显示了这个错误:
Uncaught TypeError: Cannot read property '0' of undefined(…)
未捕获类型错误:无法读取未定义(…)的属性'0'
It occurs in "data.rows[i][0]"
它发生在“data.rows[我][0]”
4 个解决方案
#1
1
You can iterate with Array#forEach():
可以使用数组#forEach()进行迭代:
var data = {"headers": [[{"text": "Code","class": "Code"}, {"text": "Code","class": "Code"}]],"rows": [[0, "Peter", 51123123],[3, "John", 90909090],[5, "Mary", 51123123]],"config": [[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],"other": [[13, 0]]},
del = 3; // Value of ID to be deleted
data.rows.forEach(function(item, index) {
item[0] === del && data.rows.splice(index, 1);
});
console.log(data.rows);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6:
ES6:
data.rows.forEach((item, index) => item[0] === del && data.rows.splice(index, 1));
#2
3
instead of using a for loop, id use the array filter function:
id不使用for循环,而是使用数组过滤器函数:
data.rows = data.rows.filter(function(row){
return row[0] !== del;
});
#3
2
Just add a break
to the condition, because the next element, is the one you have spliced, which is not anymore in the array.
只需要在条件中添加一个断点,因为下一个元素,是你已经拼接过的,它不在数组中了。
if (del == id) { // if del = id -> splice
data.rows.splice(i, 1);
break; // no more to search
}
#4
0
You can use lodash for filter your objects or arrays. Look at the filter method for your case:
可以使用lodash来过滤对象或数组。请查看您的案例的筛选方法:
var myObject = {
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
};
//filter by lodash
myObject.rows = _.filter(myObject.rows,function(row){
return row[0] !== 3;
});
#1
1
You can iterate with Array#forEach():
可以使用数组#forEach()进行迭代:
var data = {"headers": [[{"text": "Code","class": "Code"}, {"text": "Code","class": "Code"}]],"rows": [[0, "Peter", 51123123],[3, "John", 90909090],[5, "Mary", 51123123]],"config": [[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],"other": [[13, 0]]},
del = 3; // Value of ID to be deleted
data.rows.forEach(function(item, index) {
item[0] === del && data.rows.splice(index, 1);
});
console.log(data.rows);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6:
ES6:
data.rows.forEach((item, index) => item[0] === del && data.rows.splice(index, 1));
#2
3
instead of using a for loop, id use the array filter function:
id不使用for循环,而是使用数组过滤器函数:
data.rows = data.rows.filter(function(row){
return row[0] !== del;
});
#3
2
Just add a break
to the condition, because the next element, is the one you have spliced, which is not anymore in the array.
只需要在条件中添加一个断点,因为下一个元素,是你已经拼接过的,它不在数组中了。
if (del == id) { // if del = id -> splice
data.rows.splice(i, 1);
break; // no more to search
}
#4
0
You can use lodash for filter your objects or arrays. Look at the filter method for your case:
可以使用lodash来过滤对象或数组。请查看您的案例的筛选方法:
var myObject = {
"headers":[[
{"text":"Code","class":"Code"},
{"text":"Code","class":"Code"}
]],
"rows":[
[0,"Peter", 51123123],
[3,"John", 90909090],
[5,"Mary",51123123]
],
"config":[[0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],
"other":[[13,0]]
};
//filter by lodash
myObject.rows = _.filter(myObject.rows,function(row){
return row[0] !== 3;
});