[英]Remove object from array where sub-object matches a value
I'm currently attempting to sort through some values based on a query string. 我目前正在尝试根据查询字符串对一些值进行排序。 I'm able to target each
target
individually in this loop but cannot seem to properly remove them from the array. 我能够在此循环中单独定位每个
target
,但似乎无法从数组中正确删除它们。 How can I remove certain values that match on target.target
from targetList
?' 如何从
targetList
删除与target.target
匹配的某些值?
targetList.forEach(function(target, key) {
var os = getParameterByName('os');
if (os == "android") {
if (target.target.includes(":IOS")) {
targetList.splice(key, 1);
}
} else if (os == "ios") {
if (target.target.includes(":ANDROID")) {
targetList.splice(key, 1);
}
}
if (target.target.includes(":IOS")) {
target.target = schools[target.target.replace(":IOS", "").toLowerCase()] + " (iOS)" || target.target;
}
if (target.target.includes(":ANDROID")) {
target.target = schools[target.target.replace(":ANDROID", "").toLowerCase()] + " (Android)" || target.target;
}
});
targetList
contains an array like this: targetList
包含一个这样的数组:
[
{
"target": "t1",
"datapoints": [
[
51.0,
1483574400.0
],
[
54.0,
1485561600.0
],
[
54.0,
1485648000.0
]
]
},
{
"target": "t2",
"datapoints": [
[
56.0,
1484265600.0
],
[
70.0,
1484352000.0
],
[
71.0,
1484438400.0
],
[
51.0,
1484611200.0
]
]
},
]
What I'm attempting to do is that where a target matches certain criteria .includes()
I want to remove the entire containing object/array. 我正在尝试做的是在目标符合某些条件的情况下
.includes()
我想删除整个包含的对象/数组。
It seems to me your code works just fine. 在我看来,您的代码可以正常工作。 I've created a snippet where I removed the extra processing, to show that what you have already does what you ask.
我创建了一个片段,其中删除了多余的处理,以表明您已执行的操作已经完成。
Is there a reason you're doing stuff to target.target
once you've removed it from the array? 将其从数组中删除后,是否有理由对
target.target
? Do you have a reference to the containing object somewhere else? 您是否在其他地方引用了包含对象?
var targetList = [ { "target": "t1", "datapoints": [ [ 51.0, 1483574400.0 ], [ 54.0, 1485561600.0 ], [ 54.0, 1485648000.0 ] ] }, { "target": "t2", "datapoints": [ [ 56.0, 1484265600.0 ], [ 70.0, 1484352000.0 ], [ 71.0, 1484438400.0 ], [ 51.0, 1484611200.0 ] ] }, ]; targetList.forEach(function(target, key) { if (target.target.includes("t2")) { targetList.splice(key, 1); } }); console.log(targetList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.