简体   繁体   中英

Find number of occurrences of a certain value in an object - Typescript

I have a nested object and I need to calculate the number of times the 'status' field has a value of 2 and a value of -1. This is what I have.I am open to using lodash as well.

 const data = { "file1": { "file": { "path": "file1" }, "loadedBytes": 1, "status": 2, "error": null }, "file2": { "file": { "path": "file2" }, "loadedBytes": 1, "status": -1, "error": "Unexpected error occured - please try again. If error persists, please contact" }, "file3": { "file": { "path": "file3" }, "loadedBytes": 1, "status": 2, "error": null }, "file4": { "file": { "path": "file4" }, "loadedBytes": 1, "status": 2, "error": null }, "file5": { "file": { "path": "file5" }, "loadedBytes": 1, "status": -1, "error": "Unexpected error occured - please try again. If error persists, please contact Trinity" }, "file6": { "file": { "path": "file6" }, "loadedBytes": 1, "status": 1, "error": "" }, "file7": { "file": { "path": "file7" }, "loadedBytes": 1, "status": -1, "error": "Unexpected error occured - please try again. If error persists, please contact Trinity" }, "file8": { "file": { "path": "file8" }, "loadedBytes": 1, "status": 1, "error": "" }, "file9": { "file": { "path": "file9" }, "loadedBytes": 1, "status": -1, "error": "Unexpected error occured - please try again. If error persists, please contact Trinity" } } var countStatus = (status) => Object.values(data).reduce(function(n, i) { return n + (i.status === status); }, 0); console.log({ countSuccess: countStatus(2), countFailure: countStatus(-1) })

Is there a better way to achieve the same result? Please note, I am using Typescript and the addition inside the reduce method causes a type error shown here . Please advice.

The error given by TypeScript can be avoided by turning the boolean expression to number explicitly, using the unary plus:

return n + +(i.status === status);

For the rest your solution is fine.

You can use _.countBy() to get an object with the counts of all statuses, and then get the ones you want by destructuring the object ( stackblitz ):

 const data = {"file1":{"file":{"path":"file1"},"loadedBytes":1,"status":2,"error":null},"file2":{"file":{"path":"file2"},"loadedBytes":1,"status":-1,"error":"Unexpected error occured - please try again. If error persists, please contact"},"file3":{"file":{"path":"file3"},"loadedBytes":1,"status":2,"error":null},"file4":{"file":{"path":"file4"},"loadedBytes":1,"status":2,"error":null},"file5":{"file":{"path":"file5"},"loadedBytes":1,"status":-1,"error":"Unexpected error occured - please try again. If error persists, please contact Trinity"},"file6":{"file":{"path":"file6"},"loadedBytes":1,"status":1,"error":""},"file7":{"file":{"path":"file7"},"loadedBytes":1,"status":-1,"error":"Unexpected error occured - please try again. If error persists, please contact Trinity"},"file8":{"file":{"path":"file8"},"loadedBytes":1,"status":1,"error":""},"file9":{"file":{"path":"file9"},"loadedBytes":1,"status":-1,"error":"Unexpected error occured - please try again. If error persists, please contact Trinity"}} const { '2': countSuccess, '-1': countFailure } = _.countBy(data, 'status') console.log({ countSuccess, countFailure })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM