Topic Description

Create a fromPairs function that takes a single parameter pairs, which is an array of two-element subarrays. Each subarray represents a key-value pair, where the first element is the key and the second element is the value.

The fromPairs function should return a new object containing key-value pairs corresponding to each key-value pair in the pairs array.

const pairs = [
  ['explain', 'this'],
  ['help', 'you'],
  ['keep', 'growing'],
];

const pairsObject = fromPairs(pairs);
console.log(pairsObject); // => { explain: 'this', help: 'you', keep: 'growing' }

Answer


function fromPairs(pairs){
	const obj = {}
	for(const [key,value] of pairs){
		obj[key] = value
	}
	return obj
}
function fromPairs(pairs) {
  return Object.fromEntries(pairs);
}