Ruby array access 2 consecutive(chained) elements at a time
Ruby reads your mind. You want cons ecutive elements? [1, 2, 3, 4, 5, 6, 7, 8, 9].each_cons(2).to_a # => [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
Ruby reads your mind. You want cons ecutive elements? [1, 2, 3, 4, 5, 6, 7, 8, 9].each_cons(2).to_a # => [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
For your first two example cases, you could use key() and current() to assign the values you need. $ar = $o->me; // reset isn’t necessary, since you just created the array $typ = key($ar); $val = current($ar); $out = array(‘me’ => array(), ‘mytype’ => 2, ‘_php_class’ => null); $expected = [key($out), current($out)]; In those cases, … Read more
How do I loop through or enumerate a JavaScript object?