Array of object deep comparison with lodash

You can make use of differenceWith() with an isEqual() comparator, and invoke isEmpty to check if they are equal or not. var isArrayEqual = function(x, y) { return _(x).differenceWith(y, _.isEqual).isEmpty(); }; var result1 = isArrayEqual( [{a:1, b:2}, {c:3, d:4}], [{b:2, a:1}, {d:4, c:3}] ); var result2 = isArrayEqual( [{a:1, b:2, c: 1}, {c:3, d:4}], [{b:2, … Read more

How to convert array of key–value objects to array of objects with a single property?

You can use .map var data = [ {“key”:”fruit”,”value”:”apple”}, {“key”:”color”,”value”:”red”}, {“key”:”location”,”value”:”garden”} ]; var result = data.map(function (e) { var element = {}; element[e.key] = e.value; return element; }); console.log(result); also if you use ES2015 you can do it like this var result = data.map((e) => { return {[e.key]: e.value}; }); Example

Using JavaScript what’s the quickest way to recursively remove properties and values from an object?

A simple self-calling function can do it. function removeMeta(obj) { for(prop in obj) { if (prop === ‘$meta’) delete obj[prop]; else if (typeof obj[prop] === ‘object’) removeMeta(obj[prop]); } } var myObj = { “part_one”: { “name”: “My Name”, “something”: “123”, “$meta”: { “test”: “test123” } }, “part_two”: [ { “name”: “name”, “dob”: “dob”, “$meta”: { … Read more

Returning only certain properties from an array of objects in Javascript [duplicate]

This is easily done with the Array.prototype.map() function: var keyArray = objArray.map(function(item) { return item[“key”]; }); If you are going to do this often, you could write a function that abstracts away the map: function pluck(array, key) { return array.map(function(item) { return item[key]; }); } In fact, the Underscore library has a built-in function called … Read more

Search array of objects for entry matching variable and check if it matches other fields

You can use array.filter, this takes a function that is passed each item of the array and returns true or false and returns a new array that only has items where the function passed to filter will return true. You can partially apply a curried function. Example of curried function: const plus = a => … Read more

Encoding Javascript Object to Json string

Unless the variable k is defined, that’s probably what’s causing your trouble. Something like this will do what you want: var new_tweets = { }; new_tweets.k = { }; new_tweets.k.tweet_id = 98745521; new_tweets.k.user_id = 54875; new_tweets.k.data = { }; new_tweets.k.data.in_reply_to_screen_name=”other_user”; new_tweets.k.data.text=”tweet text”; // Will create the JSON string you’re looking for. var json = JSON.stringify(new_tweets); … Read more

Access numeric properties of an object using dot notation

Your question seems to be about why we can’t access array and array-like elements using the dot notation like this: const v = a.0; It’s described in the ECMAScript specification: The dot notation is explained by the following syntactic conversion: MemberExpression . IdentifierName And identifiers may not start with a digit as described here: IdentifierName … Read more