Array.every 函数并未在 Object 的所有元素上运行
Posted
技术标签:
【中文标题】Array.every 函数并未在 Object 的所有元素上运行【英文标题】:Array.every function isn't running on all elements of Object 【发布时间】:2019-08-09 11:12:59 【问题描述】:我正在尝试使用以下代码在 temp1
内部搜索是否有任何 值 具有字符串 "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"
,则 array b 会被填充,这是应该的。我认为,如果我尝试搜索存储在 keys status key 之后的 values,就会出现问题.
【问题讨论】:
只是一个建议:对于您的用例,Arrays 'find' 方法似乎是适合使用的方法。有什么理由在这里使用“每个”吗? 【参考方案1】:你需要在if (typeof value == "object")
里面return Object.values(value).every(valueOfObject....
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)
【讨论】:
【参考方案2】:为什么不考虑递归来检查任何值是否是对象?我认为它可以缩短代码并更直接。
另外,对我来说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);
【讨论】:
以上是关于Array.every 函数并未在 Object 的所有元素上运行的主要内容,如果未能解决你的问题,请参考以下文章
巧技拾遗 | JavaScript 中 Array.every 和 Array.map 的巧妙结合
数组的方法 Array.map();Array.every()和Array.some();数组的indexof();
JS Array.every 在比较字符串时总是返回 false