Reputation: 105
I'm trying to search inside temp1
if any value has the string "processado"
using the following code
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
}
})
})
But, the array b remains empty. If I try to search the string "Cobrança"
, the array b get filled, as it should. I think that if I try to search values that are stored on keys after the status key, something get wrong.
Upvotes: 0
Views: 155
Reputation: 36574
You need to return Object.values(value).every(valueOfObject....
inside 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)
A simpler and cleaner way is using recursion and filter()
and some()
. every()
doesnot make any sense to me here
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)
Upvotes: 2
Reputation: 1300
Why not consider recursion to check if any values are objects? I think it could shorten the code and be a bit more straightforward.
Also, to me Array.prototype.some
makes more sense in this case than Array.prototype.every
(unless I'm missing something):
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);
Upvotes: 1