How to set object property (of object property of..) given its string name in JavaScript?

function assign(obj, prop, value) { if (typeof prop === “string”) prop = prop.split(“.”); if (prop.length > 1) { var e = prop.shift(); assign(obj[e] = Object.prototype.toString.call(obj[e]) === “[object Object]” ? obj[e] : {}, prop, value); } else obj[prop[0]] = value; } var obj = {}, propName = “foo.bar.foobar”; assign(obj, propName, “Value”);

How to sort OrderedDict of OrderedDict?

You’ll have to create a new one since OrderedDict is sorted by insertion order. In your case the code would look like this: foo = OrderedDict(sorted(foo.items(), key=lambda x: x[1][‘depth’])) See http://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes for more examples. Note for Python 2 you will need to use .iteritems() instead of .items().

Return value from nested function in Javascript [duplicate]

you have to call a function before it can return anything. function mainFunction() { function subFunction() { var str = “foo”; return str; } return subFunction(); } var test = mainFunction(); alert(test); Or: function mainFunction() { function subFunction() { var str = “foo”; return str; } return subFunction; } var test = mainFunction(); alert( test() … Read more

What happens when nesting elements with position: fixed inside each other?

The fixing and the positioning are two separate things. They’re positioned the same as absolutely positioned elements: relative to their containing block. But in contrast with absolutely positioned elements, they remain fixed to that position with respect to the viewport (i.e. they don’t move when scrolling): http://www.w3.org/TR/CSS2/visuren.html#propdef-position The box’s position is calculated according to the … Read more

Nested ifelse statement

If you are using any spreadsheet application there is a basic function if() with syntax: if(<condition>, <yes>, <no>) Syntax is exactly the same for ifelse() in R: ifelse(<condition>, <yes>, <no>) The only difference to if() in spreadsheet application is that R ifelse() is vectorized (takes vectors as input and return vector on output). Consider the … Read more

Styling nested elements in WPF

Just to complete the original answer, I think it is clearer adding the nested style inside the parent like that: <Style x:Key=”WindowHeader” TargetType=”DockPanel” > <Setter Property=”Background” Value=”AntiqueWhite”></Setter> <Style.Resources> <Style TargetType=”Image”> <Setter Property=”Margin” Value=”6″></Setter> <Setter Property=”Width” Value=”36″></Setter> <Setter Property=”Height” Value=”36″></Setter> </Style> <Style TargetType=”TextBlock”> <Setter Property=”TextWrapping” Value=”Wrap”></Setter> </Style> </Style.Resources> </Style>

accepts_nested_attributes_for child association validation failing

Use :inverse_of and validates_presence_of :parent. This should fix your validation problem. class Dungeon < ActiveRecord::Base has_many :traps, :inverse_of => :dungeon end class Trap < ActiveRecord::Base belongs_to :dungeon, :inverse_of => :traps validates_presence_of :dungeon end http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_presence_of https://github.com/rails/rails/blob/73f2d37505025a446bb5314a090f412d0fceb8ca/activerecord/test/cases/nested_attributes_test.rb

How to yield results from a nested generator function?

You may have to use the new yield from, available since Python 3.3, known as “delegated generator”. If I understood the question correctly, I came to the same issue, and found an answer elsewhere. I wanted to do something like this: def f(): def g(): do_something() yield x … yield y do_some_other_thing() yield a … … Read more