Issue
How do we filter an object in angular or typescript to remove object that has an empty value for example remove object where annualRent === null . Also what rounding solution can we use to round for example 2.8333333333333335 to 2.83 and remove the other additional decimals like 3333 ....
Thanks.
what I tried and have in mind , something like
array.filter((x): x is MyType => x !== null);
#sample object
[
{
"id": 0,
"type": 0,
"startDate": "2021-09-27T16:00:00.000Z",
"endDate": "2021-09-29T16:00:00.000Z",
"endDateString": "09/30/2021",
"annualRent": "23232",
"endDateError": null
},
{
"id": 1,
"startDate": "2021-09-27T16:00:00.000Z",
"endDate": "2021-09-29T16:00:00.000Z",
"endDateString": null,
"annualRent": null,
"prevEndDate": "2021-09-29T16:00:00.000Z",
"useFMV": false
}
]
Solution
You can use the filter method as follows:
arr = [
{
"id": 0,
"type": 0,
"startDate": "2021-09-27T16:00:00.000Z",
"endDate": "2021-09-29T16:00:00.000Z",
"endDateString": "09/30/2021",
"annualRent": "23232",
"endDateError": null
},
{
"id": 1,
"startDate": "2021-09-27T16:00:00.000Z",
"endDate": "2021-09-29T16:00:00.000Z",
"endDateString": null,
"annualRent": null,
"prevEndDate": "2021-09-29T16:00:00.000Z",
"useFMV": false
}
].filter(obj=> obj.annualRent !== null);
console.log(arr);
You can use the method toFixed() to get the number of digits as follows
let num = 2.8333333333333335;
let roundedNum = num.toFixed(2)
console.log(roundedNum);
Answered By - deepakchethan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.