This is a project task. Please refrain from down voting. Everyone starts somewhere, and we all have unique ways of learning.
The requirement is for the function to accept a 2D array (an array of pairs) in this specific format:
array = [[1, 2], [3, 4], ['nice', 'free'], [5, 6]];
The array can vary in length, but must always be in pairs as demonstrated above.
The function should return: {1:2, 3:4, nice: 'free', 5:6}
Here is the code snippet I've come up with so far:
function keyValue(array) {
for (var i = 0; i<array.length; i++){
var pairs = {
[array[i][0]]: array[i][1]
};
console.log(pairs);
}
}
keyValue([[1, 2], [3, 4], ['nice', 'free'], [5, 6]]);
The output displays: keyValue
(array)'returns':Object {1: 2}, Object {3: 4}, Object {nice: 'free'}, Object {5: 6}
'console.log' shows all key-value pairs, while 'return' only presents the first pair; for example {1:2}
I am unsure whether I created multiple objects with their own key-values, hence why 'return' only showed one pair
Alternatively, if I indeed created just one object, I need to utilize the 'return' function to display the complete set of key-values within that object. Any assistance would be greatly appreciated. Thank you in advance.