Write a function that takes an array of objects, and groups them into an object whose keys are the unique values of a specified property on each object, and whose values are arrays of objects with that value for that property. For example, given the input array
[{name: 'Alice', age: 25}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}]
and the property 'age', the function should return.
{25: [{name: 'Alice', age: 25}, {name: 'Bob', age: 25}], 30: [{name: 'Charlie', age: 30}]}
- Write a function that returns true if all the objects have a property named age that is greater than 18. The function should return false as soon as it finds the first object that does not satisfy this condition, without checking the remaining objects in the array.
Write a function that takes an array of arrays, and returns the Cartesian product of those arrays. The Cartesian product of two arrays is the set of all possible pairs where the first element comes from the first array, and the second element comes from the second array.
For example, given the input [[1, 2], [3, 4]]
, the function should return [[1, 3], [1, 4], [2, 3], [2, 4]]
.
Write a function that takes two arrays of objects, and returns an array of objects that represent the "join" between the two arrays. The join between two objects is an object that contains the keys present in both objects, and the values are the values of those keys from both objects. For example, given the input arrays [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}] and [{id: 1, age: 25}, {id: 3, age: 30}], the function should return [{id: 1, name: 'Alice', age: 25}].
Implement a function that performs binary search on an array of numbers. The function should take in a sorted array of integers and a target integer to find. It returns the index of the target element or -1, if the target element doesn't exist in the array.
Binary search is a search algorithm that can efficiently determine if a sorted array of integers contain a specific number. The algorithm repeatedly divides the input array into half until the target element is found, thereby decreasing the search space by half each step. It is a significant improvement versus linear search.
Here is a quick explanation of how binary search works on an array that is already sorted:
- Calculate the middle index of the array and retrieve the middle element.
- If the target element is greater than the middle element, search the right half of the array (ignore the left half).
- If the target element is lesser than the middle element, search the left half of the array.
- If the target element is equal to the middle element, return the index of the element.
- Repeat the steps above until we complete the search. Return -1 if the target was not found.