[英]Remove an item from array of items
I have list of variables/items 我有变量/物品清单
dbTemp = [Type,Threshold,TypeID,Prioirty,Value,Assign]
where Type,Threshold,TypeID,Prioirty
are variables Let's say, their values are 1,0,2,0,NULL,21
其中Type,Threshold,TypeID,Prioirty
是变量假设它们的值为1,0,2,0,NULL,21
If a variable value is 0
or NULL
, I need to remove/exclude them from the list and build a dynamic variable expression based on the non-zero or non-NULL values 如果变量值为0
或NULL
,则需要从列表中删除/排除它们,并基于非零或非NULL值构建动态变量表达式
In this case, Dynamic Expression = Type>0+TypeID>0+Assign>0
(excludes Threshold
, Priority
, Value
variables since their values are 0
or NULL
) 在这种情况下, Dynamic Expression = Type>0+TypeID>0+Assign>0
(排除Threshold
, Priority
, Value
变量,因为它们的值为0
或NULL
)
Can you please help me here? 你能在这里帮我吗?
filtered here: 在这里过滤:
var filtered = dbTemp.filter( function(el) { return !!el; } );
will contain all non null or zero elements from dbTemp; 将包含来自dbTemp的所有非null或零元素;
If you want to filter based on some condition(s) instead of just filtering falsy values: 如果要基于某些条件进行过滤,而不仅仅是过滤虚假值,请执行以下操作:
var filtered = orignalArray.filter(function(item) {
return (item !== condition1) && (item !== condition2);
});
ps NULL !== null ps NULL!==空
If you'll allow for any falsey value and a result that is a filtered copy, then this will suffice: 如果您允许任何假值和过滤后的副本,那么就足够了:
var result = dbTemp.filter(Boolean);
If you actually need to mutate the original and order doesn't matter, then do this: 如果您实际上需要更改原始文件而顺序无关紧要,请执行以下操作:
for (var i = 0; i < dbTemp.length; i++) {
if (!dbTemp[i]) {
dbTemp[i] = dbTemp[dbTemp.length-1];
dbTemp.length--;
i--;
}
}
If the original order does matter, then this: 如果原始订单确实很重要,则可以这样做:
for (var i = 0; i < dbTemp.length; i++) {
if (!dbTemp[i]) {
dbTemp[i].splice(i, 1);
i--;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.