删除 javascript 个不匹配字符串的对象属性

Deleting javascript object properties not matching a string

我有一个看起来类似于此的 javascript 对象:

object {
  attribute[1424]1405: 149,
  attribute[1425]1406: 149,
  attribute[1426]1407: 149,
  attribute[1426]1408: 149,
  attribute[1649]2116: 149,
  attribute[1649]2117: 179,
  attribute[1649]2408: 119
}

我正在尝试删除所有不以 attribute[1649] 开头的属性(存储在名为 conditionID 的变量中)。是否有某种类似于 !:contains() 的过滤器,我可以使用 delete 命令针对对象 运行?

不,没有。只需使用常规 for in 循环:

for (var p in obj) 
    if (!/^attribute\[1649\]/.test(p))
        delete obj[p];

(请参阅 How to check if a string "StartsWith" another string? 了解正则表达式的替代方法)