Posts

Showing posts with the label dictionary

find duplicate obj properties and return lowest count property

find duplicate obj properties and return lowest count property Lets say I had an array that looked like: [ {count: 1, category: 4}, {count: 2, category: 4}, {count: 3, category: 2}, {count: 4, category: 2}, {count: 5, category: 8}, {count: 6, category: 8}, {count: 7, category: 1}, {count: 8, category: 1}, {count: 9, category: 1} {count: 10, category: 8}, ... ] What I want is to find the lowest count from each category and return a new array of objects. I could easily do this using a plain old loop i think, but would like to use map().reduce or some other new func technique. This should help stackoverflow.com/q/14446511/831878 – Ray Toal Jul 2 at 0:07 4 Answers 4 There are many ways to do it. One would be: function filterLowestCounts(a) { const lowestCo...

Python - Flatting Dictionary where some keys are 'missing'

Python - Flatting Dictionary where some keys are 'missing' Is there a cleaner way to do this? I am checking a nested dictionary for a specific key and assigning that to a variable. I will be using it to make a mysql insert shortly. My main issue is that if the key is not present (which sometimes happens) I dont want the code to fail, I just want to sent the variable to empty. This code seems to work but I have to do something similar to about 20 differnt key values so I would like to make sure I am doing it the best way if 'Model' in ItemAttributes: Model=ItemAttributes['Model']['value'] else: Model='' 3 Answers 3 Here is one solution: Model = ItemAttributes.get('Model', {}).get('value', '') If the "Model" key does not exist in the ItemAttributes dictionary, you return an empty dictionary, on which you use dict...