Javascript 如何从递归$中的对象中删除元素。每个?
例如,我有一个数据对象:Javascript 如何从递归$中的对象中删除元素。每个?,javascript,jquery,Javascript,Jquery,例如,我有一个数据对象: var data = { 'some': [ {'id': 10, 'info': [{'next': 11}, {'next': 0}, {'next': 0}, {'next': 0}]}, {'id': 11, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]}, {'id': 12, 'info': [{'next': 0
var data = {
'some': [
{'id': 10, 'info': [{'next': 11}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 11, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 12, 'info': [{'next': 0}, {'next': 0}, {'next': 20}, {'next': 0}]},
{'id': 13, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 14, 'info': [{'next': 12}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 15, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 16, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 14}]},
{'id': 17, 'info': [{'next': 0}, {'next': 0}, {'next': 13}, {'next': 0}]},
{'id': 18, 'info': [{'next': 15}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 19, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 19, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 11}]},
{'id': 20, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]}
]
};
我想删除id
中包含在next
中的所有元素,例如,从上面的数据对象中,我想得到如下内容
var data = {
'some': [
{'id': 10, 'info': [{'next': 11}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 16, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 14}]},
{'id': 17, 'info': [{'next': 0}, {'next': 0}, {'next': 13}, {'next': 0}]},
{'id': 18, 'info': [{'next': 15}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 19, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 0}]},
{'id': 19, 'info': [{'next': 0}, {'next': 0}, {'next': 0}, {'next': 11}]},
]
};
我写了这个脚本,但它返回了未捕获的TypeError:无法读取未定义的属性'id',为什么
var cleanData=函数(d
谢谢。当长度大于零时使用a,并从数组中删除索引1当长度大于零时使用a,并从数组中删除索引1当长度大于零时使用a,并从数组中删除索引1当长度大于零时使用a,并从数组中删除索引1y您得到的错误是因为并非数组中的每个对象都具有键为“id”的属性
我不太清楚应该删除什么,但如果是所有内容,第一个键是“下一步”,请更改此键:
if (some2['id'] == next['next'])
为此:
if (some2[0] == 'next')
嗯,
-Ted您得到的错误是因为并非数组中的每个对象都具有键为“id”的属性
我不太清楚应该删除什么,但如果是所有内容,第一个键是“下一步”,请更改此键:
if (some2['id'] == next['next'])
为此:
if (some2[0] == 'next')
嗯,
-Ted您得到的错误是因为并非数组中的每个对象都具有键为“id”的属性
我不太清楚应该删除什么,但如果是所有内容,第一个键是“下一步”,请更改此键:
if (some2['id'] == next['next'])
为此:
if (some2[0] == 'next')
嗯,
-Ted您得到的错误是因为并非数组中的每个对象都具有键为“id”的属性
我不太清楚应该删除什么,但如果是所有内容,第一个键是“下一步”,请更改此键:
if (some2['id'] == next['next'])
为此:
if (some2[0] == 'next')
嗯,
-Ted更新:这是一个更清洁、更安全的解决方案。
简而言之,您不能。您必须使用带有索引的常规for
循环,以便跟上数组的新长度
$.each(data['some'], function (_is, some) {
$.each(some['info'], function (_in, next) {
if (next['next'])
for(var i=0, len=data['some'].length; i < len; i++) {
var some2 = data['some'][i];
if (some2['id'] == next['next']) {
data['some'].remove(_is2);
// Decrement i since the next item will now be in the place of the one you removed.
i--;
// Decrement len since your array now holds one less item.
len--;
}
}
}
});
});
$。每个(数据['some'],函数(\u is,some){
$.each(一些['info'],函数(_in,next){
如果(下一个['next']))
对于(变量i=0,len=data['some'].length;i
更新:这是一个更清洁、更安全的解决方案。
简而言之,您不能。您必须使用带有索引的常规for
循环,以便跟上数组的新长度
$.each(data['some'], function (_is, some) {
$.each(some['info'], function (_in, next) {
if (next['next'])
for(var i=0, len=data['some'].length; i < len; i++) {
var some2 = data['some'][i];
if (some2['id'] == next['next']) {
data['some'].remove(_is2);
// Decrement i since the next item will now be in the place of the one you removed.
i--;
// Decrement len since your array now holds one less item.
len--;
}
}
}
});
});
$。每个(数据['some'],函数(\u is,some){
$.each(一些['info'],函数(_in,next){
如果(下一个['next']))
对于(变量i=0,len=data['some'].length;i
更新:这是一个更清洁、更安全的解决方案。
简而言之,您不能。您必须使用带有索引的常规for
循环,以便跟上数组的新长度
$.each(data['some'], function (_is, some) {
$.each(some['info'], function (_in, next) {
if (next['next'])
for(var i=0, len=data['some'].length; i < len; i++) {
var some2 = data['some'][i];
if (some2['id'] == next['next']) {
data['some'].remove(_is2);
// Decrement i since the next item will now be in the place of the one you removed.
i--;
// Decrement len since your array now holds one less item.
len--;
}
}
}
});
});
$。每个(数据['some'],函数(\u is,some){
$.each(一些['info'],函数(_in,next){
如果(下一个['next']))
对于(变量i=0,len=data['some'].length;i
更新:这是一个更清洁、更安全的解决方案。
简而言之,您不能。您必须使用带有索引的常规for
循环,以便跟上数组的新长度
$.each(data['some'], function (_is, some) {
$.each(some['info'], function (_in, next) {
if (next['next'])
for(var i=0, len=data['some'].length; i < len; i++) {
var some2 = data['some'][i];
if (some2['id'] == next['next']) {
data['some'].remove(_is2);
// Decrement i since the next item will now be in the place of the one you removed.
i--;
// Decrement len since your array now holds one less item.
len--;
}
}
}
});
});
$。每个(数据['some'],函数(\u is,some){
$.each(一些['info'],函数(_in,next){
如果(下一个['next']))
对于(变量i=0,len=data['some'].length;i
Array.prototype.filter非常适合解决此类问题。此代码应能生成您所需的:
data.some = data.some.filter(function(item) {
return item.info.filter(function(i) {
return i.next > 0;
}).length > 0;
});
Array.prototype.filter非常适合处理此类问题。此代码应能生成您需要的内容:
data.some = data.some.filter(function(item) {
return item.info.filter(function(i) {
return i.next > 0;
}).length > 0;
});
Array.prototype.filter非常适合处理此类问题。此代码应能生成您需要的内容:
data.some = data.some.filter(function(item) {
return item.info.filter(function(i) {
return i.next > 0;
}).length > 0;
});
Array.prototype.filter非常适合处理此类问题。此代码应能生成您需要的内容:
data.some = data.some.filter(function(item) {
return item.info.filter(function(i) {
return i.next > 0;
}).length > 0;
});
您可以通过两个步骤创建它:首先查找任何下一个对象中包含的所有ID。然后过滤数组并删除带有该ID的行