Issue
So I'm Learning javascripts array functions and found one solution too but it is using Object.fromEntries but in my angular project I have old es version and cant update it due to some reason.
so the problem is I have one array of object which is
var a =
[{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": true,
"id":""
},
{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 45454,
"code": "2121212",
"amount": "",
"isTaxDetails": true,
"id":""
}]
and I want to check all object should have value in all keys except key "id"
so I was using below code to achieve it
a.map((ele: any) => Object.fromEntries(
Object.entries(ele)
.filter(([key, val]) => key != "id" && val)
));
still I dont get the desired result as
[{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": true,
"id":""
}]
below is the desired output
[{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": true,
"id":""
}]
only one object bcz all key contains value expect id key
which is wrong. So any javascript function which can help?
Solution
You can use
var a =
[{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": true,
"id":""
}, {
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": null,
"id":""
},
{
"dateOfDeposit": "2022-06-08T18:30:00.000Z",
"cNumber": 44444,
"code": "5555555",
"amount": "5,555",
"isTaxDetails": 0,
"id":""
}];
var result = a.filter(function(item){
return Object.entries(item).every(function([key, val]){
return key === "id" || (val != null && val !== "");
})
})
console.log(result);
Answered By - Abdullah Ansari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.