Setting an ES6 class getter to enumerable

ES6 style getters are defined on the prototype, not on each individual person. To set the greeting property to enumerable you need to change: // Make enumerable (doesn’t work) Object.defineProperty(Person, ‘greeting’, {enumerable: true}); To: // Make enumerable Object.defineProperty(Person.prototype, ‘greeting’, {enumerable: true}); Object.keys only returns that object’s own enumerable properties, so properties on the prototype are … Read more

What is the most efficient way to copy some properties from an object in JavaScript?

You can achieve it with a form of destructuring: const user = { _id: 1234, firstName: ‘John’, lastName: ‘Smith’ }; const { _id, …newUser } = user; console.debug(newUser); However, at the time of writing this answer, the spread (…) syntax is still at the ECMAScript proposal stage (stage 3), so it may not be universally … Read more

Get list of duplicate objects in an array of objects

You can use Array#reduce to make a counter lookup table based on the id key, then use Array#filter to remove any items that appeared only once in the lookup table. Time complexity is O(n). const values = [{id: 10, name: ‘someName1’}, {id: 10, name: ‘someName2′}, {id: 11, name:’someName3’}, {id: 12, name: ‘someName4’}]; const lookup = … Read more

Is there a standardized ES6 file extension? If so, what is it?

There’s no formal ES6/JS extension, although majority of people seem to prefer .js. ECMAScript specific suffixes aren’t common. Mozilla is using two extensions within Firefox and FirefoxOS: .js and .jsm. No ECMA Script specific suffixes. For Gecko (the layout engine written largely in JS), they use both .js and .jsm. Example: one of the DOM … Read more

Array.fill(Array) creates copies by references not by value [duplicate]

You could use Array.from() instead: Thanks to Pranav C Balan in the comments for the suggestion on further improving this. let m = Array.from({length: 6}, e => Array(12).fill(0)); m[0][0] = 1; console.log(m[0][0]); // Expecting 1 console.log(m[0][1]); // Expecting 0 console.log(m[1][0]); // Expecting 0 Original Statement (Better optimized above): let m = Array.from({length: 6}, e => … Read more