So if you were going to take that approach here you can start with the map method. Whenever that red flag of iterating over an array comes up I immediatly ask myself is this something that map/filter/reduce can solve. What if you have one thousand or a million objects in the array? Any time that I start iterating over an array using some type of loop to perform som calculation that is a huge red flag for me. If you were to run this code it certainly works but it just doesn't seem right to me. My main objective every time I write code is to get something to work first and then improve upon it later. const characters = [ ) assert ( max = 444 ) Each character in the array is going to be an object that contains an id, first name and last name. Side note: If you haven't read that book yet, I highly recommend it. I recently read the book Bad Blood so I decided to use some of the characters from that book. Next you are going to create an array of characters. The first thing that I am going to do is to require the assert module from Node which gives us the ability to provide a simple set of assertion tests. I am using Node to run this example but you can just as easily use JavaScript and drop this into a web page. In this article I will walk you through a 4 different solutions. I wanted a quick and efficient way to look at all of the ids and find the max id. I had this array of objects and in each object I had an id. I was recently putting together a tutorial for some students and I came across an issue that I am sure we have all come across at one time or another. How to find the max id in an array of objects in JavaScript
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |