Javascript – removing undefined fields from an object [duplicate]

A one-liner using ES6 arrow function and ternary operator: Object.keys(obj).forEach(key => obj[key] === undefined ? delete obj[key] : {}); Or use short-circuit evaluation instead of ternary: (@Matt Langlois, thanks for the info!) Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key]) Same example using if statement: Object.keys(obj).forEach(key => { if (obj[key] === undefined) { delete obj[key]; … Read more

Doctrine 2 mysql FIELD function in order by

Jeremy Hicks, thanks for your extension. I didn`t know how to connect your function to doctrine, but finally i find answer. $doctrineConfig = $this->em->getConfiguration(); $doctrineConfig->addCustomStringFunction(‘FIELD’, ‘DoctrineExtensions\Query\Mysql\Field’); I need FIELD function to order my Entities that i select by IN expression. But you can use this function only in SELECT, WHERE, BETWEEN clause, not in ORDER … Read more

c# – How to iterate through classes fields and set properties

Here’s a complete working example: public class Person { public string Name { get; set; } } class Program { static void PropertySet(object p, string propName, object value) { Type t = p.GetType(); PropertyInfo info = t.GetProperty(propName); if (info == null) return; if (!info.CanWrite) return; info.SetValue(p, value, null); } static void PropertySetLooping(object p, string propName, … Read more

Any reason to use auto-implemented properties over manual implemented properties?

It doesn’t grant you anything extra beyond being concise. If you prefer the more verbose syntax, then by all means, use that. One advantage to using auto props is that it can potentially save you from making a silly coding mistake such as accidentally assigning the wrong private variable to a property. Trust me, I’ve … Read more

How to avoid sending input fields which are hidden by display:none to a server?

Setting a form element to disabled will stop it going to the server, e.g.: <input disabled=”disabled” type=”text” name=”test”/> In javascript it would mean something like this: var inputs = document.getElementsByTagName(‘input’); for(var i = 0;i < inputs.length; i++) { if(inputs[i].style.display == ‘none’) { inputs[i].disabled = true; } } document.forms[0].submit(); In jQuery: $(‘form > input:hidden’).attr(“disabled”,true); $(‘form’).submit();