Convert Array to Object

ECMAScript 6 introduces the easily polyfillable Object.assign: The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. Object.assign({}, [‘a’,’b’,’c’]); // {0:”a”, 1:”b”, 2:”c”} The own length property of the array is not copied because it … Read more

How to remove all duplicates from an array of objects?

How about with some es6 magic? obj.arr = obj.arr.filter((value, index, self) => index === self.findIndex((t) => ( t.place === value.place && t.name === value.name )) ) Reference URL A more generic solution would be: const uniqueArray = obj.arr.filter((value, index) => { const _value = JSON.stringify(value); return index === obj.arr.findIndex(obj => { return JSON.stringify(obj) === _value; … Read more

How to check if object property exists with a variable holding the property name?

var myProp = ‘prop’; if(myObj.hasOwnProperty(myProp)){ alert(“yes, i have that property”); } Or var myProp = ‘prop’; if(myProp in myObj){ alert(“yes, i have that property”); } Or if(‘prop’ in myObj){ alert(“yes, i have that property”); } Note that hasOwnProperty doesn’t check for inherited properties, whereas in does. For example ‘constructor’ in myObj is true, but myObj.hasOwnProperty(‘constructor’) … Read more

How to access the first property of a Javascript object?

var obj = { first: ‘someVal’ }; obj[Object.keys(obj)[0]]; //returns ‘someVal’ Object.values(obj)[0]; // returns ‘someVal’ Using this you can access also other properties by indexes. Be aware tho! Object.keys or Object.values return order is not guaranteed as per ECMAScript however unofficially it is by all major browsers implementations, please read https://stackoverflow.com/a/23202095 for details on this.

Most efficient method to groupby on an array of objects

If you want to avoid external libraries, you can concisely implement a vanilla version of groupBy() like so: var groupBy = function(xs, key) { return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); }; console.log(groupBy([‘one’, ‘two’, ‘three’], ‘length’)); // => {3: [“one”, “two”], 5: [“three”]}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)