How do I check if an array includes an object in JavaScript? What is the most concise and efficient way to find out if a JavaScript array contains an object? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return true; } } return false; } Is there a better and more concise way to accomplish this? This is very closely related to Stack
undefined
How can I merge PHP arrays? I have two arrays of animals (for example). $array = array( array( 'id' => 1, 'name' => 'Cat', ), array( 'id' => 2, 'name' => 'Mouse', ) ); $array2 = array( array( 'id' => 2, 'age' => 321, ), array( 'id' => 1, 'age' => 123, ) ); How can I merge the two arrays into one by the ID? Answer: This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results. $results =
undefined
PHP array indexing: $array[$index] vs $array[“$index”] vs $array[“{$index}”] What is the difference, if any, between these methods of indexing into a PHP array: $array[$index] $array["$index"] $array["{$index}"] I'm interested in both the performance and functional differences. Update: I'm not sure that's right. I ran this code: $array = array(100, 200, 300); print_r($array); $idx = 0; $array[$idx] = 123; print_r($array); $array["$idx"] = 456; print_r($array); $array["{$idx}"] = 789; print_r($array); And got this output: Array ( [0] => 100 [1] => 200 [2] => 300 ) Array
undefined
How to pick latest item per user from an array in Javascript? For example, I have an array like this: [ { "data": "some data", "user": "A", "createdAt":"2016-12-24", }, { "data": "some data2", "user": "B", "createdAt":"2016-12-24", }, { "data": "some data3", "user": "A", "createdAt":"2016-12-25", }, { "data": "some data4", "user": "A", "createdAt":"2016-12-23", }, { "data": "some data5", "user": "B", "createdAt":"2016-12-25", }, { "data": "some data6", "user": "C", "createdAt":"2016-12-24", }, ] I only need the latest created item per one user, so I need an