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

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