JavaScript: Remove duplicates of objects sharing same property value

This function removes duplicate values from an array by returning a new one. function removeDuplicatesBy(keyFn, array) { var mySet = new Set(); return array.filter(function(x) { var key = keyFn(x), isNew = !mySet.has(key); if (isNew) mySet.add(key); return isNew; }); } var values = [{color: “red”}, {color: “blue”}, {color: “red”, number: 2}]; var withoutDuplicates = removeDuplicatesBy(x => … Read more

JSON to Javascript Class

Just assign to an instance: static from(json){ return Object.assign(new Student(), json); } So you can do: const student = Student.from({ name: “whatever” }); Or make it an instance method and leave away the assignemnt: applyData(json) { Object.assign(this, json); } So you can: const student = new Student; student.applyData({ name: “whatever” }); It could also be … Read more

How to change HTML Object element data attribute value in javascript

This works: <html> <head></head> <body> <object type=”text/html” id=”htmlFrame” style=”border: none;” standby=”loading” width=”100%”></object> <script type=”text/javascript”> var element = document.getElementById(“htmlFrame”); element.setAttribute(“data”, “attributeValue”); </script> </body> </html> If you put this in a file, open in it a web browser, the javascript will execute and and the “data” attribute + value will be added to the object element. Note: … Read more

jquery/javascript convert date string to date

If you’re running with jQuery you can use the datepicker UI library’s parseDate function to convert your string to a date: var d = $.datepicker.parseDate(“DD, MM dd, yy”, “Sunday, February 28, 2010”); and then follow it up with the formatDate method to get it to the string format you want var datestrInNewFormat = $.datepicker.formatDate( “mm/dd/yy”, … Read more

Is it possible to use a c# object initializer with a factory method?

No. Alternatively you could accept a lambda as an argument, which also gives you full control in which part of the “creation” process will be called. This way you can call it like: MyClass instance = MyClass.FactoryCreate(c=> { c.SomeProperty = something; c.AnotherProperty = somethingElse; }); The create would look similar to: public static MyClass FactoryCreate(Action<MyClass> … Read more

Casting an array of Objects into an array of my intended class

Because toArray() creates an array of Object, and you can’t make Object[] into DataObject[] just by casting it. toArray(DataObject[]) creates an array of DataObject. And yes, it is a shortcoming of the Collections class and the way Generics were shoehorned into Java. You’d expect that Collection<E>.toArray() could return an array of E, but it doesn’t. … Read more

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