Array.every 函数不是 运行 对象的所有元素

Array.every function isn't running on all elements of Object

我正在尝试在 temp1 中搜索是否有任何 value 具有字符串 "processado" 使用以下代码

let temp1 = [{
    "id":7089,
    "value":"R$ 50,00",
    "name":"Daiany Nascimento",
    "date":"18/03/2019",
    "type":"Cobrança",
    "status":{
        "status":"Paga",
        "icon":"paid"
    },
    "credit_release_date":"Não Processado",
    "credit_release_description":"— — — —"
}]

let b = []

temp1.forEach((a,index_a) => { 
Object.values(a).every((value,index,array) => {
    let expression = new RegExp("processado", "i") //expression to search
    if (typeof value == "object") {
      Object.values(value).every(valueOfObject => {    
        if (expression.test(valueOfObject)) {
          b.push(temp1[index_a])
          return false;
        } else {
          return true
        }
      })
    }
    else if (expression.test(value)){ 
      b.push(temp1[index_a])
      return false
    }
      else {
      return true
    }
  })
})

但是,数组 b 仍然是空的。如果我尝试搜索字符串 "Cobrança" 数组 b 会被填充,这是应该的。我认为如果我尝试搜索存储在 keys after 上的 values 状态键,出错了。

你需要return Object.values(value).every(valueOfObject....里面if (typeof value == "object")

let temp1 = [{"id":7089,"value":"R$ 50,00","name":"Daiany Nascimento","date":"18/03/2019","type":"Cobrança","status":{"status":"Paga","icon":"paid"},"credit_release_date":"Não Processado","credit_release_description":"— — — —"}]

let b = []

temp1.forEach((a,index_a) => { 
Object.values(a).every((value,index,array) => {
    let expression = new RegExp("processado", "i") //expression to search
    if (typeof value == "object") {
      return Object.values(value).every(valueOfObject => {    
        if (expression.test(valueOfObject)) {
          b.push(temp1[index_a])
          return false;
        } else {
          return true
        }
      })
    }
      else if (expression.test(value)){ 
      b.push(temp1[index_a])
      return false
      }
      else {
      return true
      }
  })
})
console.log(b)

更简单、更简洁的方法是使用递归和 filter()some()every() 在这里对我没有任何意义

let temp1 = [{"id":7089,"value":"R$ 50,00","name":"Daiany Nascimento","date":"18/03/2019","type":"Cobrança","status":{"status":"Paga","icon":"paid"},"credit_release_date":"Não Processado","credit_release_description":"— — — —"}]

function check(obj,regex){
  return Object.values(obj).some(x =>{
      let y;
      if(typeof x === "object") y = check(x,regex);
      return y || regex.test(x);
   });
}

let b = temp1.filter(x => check(x,/Processado/i))
console.log(b)

为什么不考虑递归来检查是否有任何值是对象?我认为它可以缩短代码并更直接一些。

此外,对我来说 Array.prototype.some 在这种情况下比 Array.prototype.every 更有意义(除非我遗漏了什么):

const temp1 = [{
  "id": 7089,
  "value": "R$ 50,00",
  "name": "Daiany Nascimento",
  "date": "18/03/2019",
  "type": "Cobrança",
  "status": {
    "status":"Paga",
    "icon":"paid"
  },
  "credit_release_date": "Não Processado",
  "credit_release_description": "— — — —"
}];

const b = [];

const expression = new RegExp('processado', 'i');

const hasExpr = obj => Object.values(obj).some((value, i) => {
  if (typeof value === 'object')
    return hasExpr(value);
  return expression.test(value);
});

temp1.forEach(item => {
  if (hasExpr(item))
    b.push(item);
});

console.log(b);