Checking ‘undefined’ or ‘null’ of any Object

I am working on Angular project and time to time I used to have check undefined or null over Object or it’s properties. Normally I use lodash _.isUndefined() see example below: I couldn’t see any problem with it. But I had discussion with my colleague during review of above code. He was telling me that if i gets undefined before the if statement then it will throw the exception. Instead he suggested me to always check i or like this: I am convinced what he was trying to say unlike his way of checking undefined in above code. But

Nested grouping in array of objects with id and date?

I have an array of json objects as Now i want to group data for same accountid for a year. For example Please guide me how can achieve this ? Answer Use forEach over items and Build an object with keys and accountId (have separate buckets for each month). Get Object.values from the object just built.

How to merge two objects, overriding null values?

I’d like to merge two similar but not identical objects and override null values in one of them, if such exist. For example I’d have these two objects: And the effect of merge should be: In other words, the most important source of data in the merged object is obj2 but it lacks some properties from obj1, so they need to be copied and also some of the obj2 values are null so they should be taken from obj1 as well. EDIT I tried: and Answer You can use _.mergeWith(), and in the merge callback only take the 2nd value

Conditionally concatenate in reducer using lodash

I try to conditionally concat in my reducer, I have an array objects and I want to concatenate only if value not exist in my reducer. If value exists nothing must happen. I use lodash and I tried with _.uniqBy like this : This does not work. Example of my reducer jsFiddle linkk Required output : Because val1 ({id:1, name:’alex’}) already exists in the array. Answer Concatenating the arrays, and removing duplicates using _.uniqBy() works fine. Example: However, it’s better to use _.unionBy() which creates an array of unique values, in order, from all given arrays:

Terse way to intersperse element between all elements in JavaScript array?

Say I have an array var arr = [1, 2, 3], and I want to separate each element by an element eg. var sep = “&”, so the output is [1, “&”, 2, “&”, 3]. Another way to think about it is I want to do Array.prototype.join (arr.join(sep)) without the result being a string (because the elements and separator I am trying to use are Objects, not strings). Is there a functional/nice/elegant way to do this in either es6/7 or lodash without something that feels clunky like: or or even Edit: Haskell has this function, called intersperse Answer Using a

Convert returned JSON Object Properties to (lower first) camelCase

I have JSON returned from an API like so: To keep this consistent with my code style (camelCase – lower case first letter) I want to transform the array to produce the following: What’s the easiest/best way to do this? Create a new Contact object and iterate over all the contacts in the returned array? or can I map the original array or transform it somehow? Answer Here’s a reliable, recursive function that will properly camelCase all of a JavaScript object’s properties: Test: Output: