Hard

Transforming Object Properties in JavaScript

Description

In JavaScript, objects are often used to store and manipulate data. Sometimes, you need to transform the properties of an object dynamically, such as renaming keys, modifying values, or filtering out certain properties.

In this challenge, you will write a function named transformObject that takes an object and a transformation function as input.

The transformation function will define how each key-value pair in the object should be transformed.

Your task is to apply this transformation to every property in the object and return a new object with the transformed properties.

Example Usage:

const original = {
name: "Alice",
age: 25,
occupation: "Software Engineer"
};
// Transformation function: capitalize keys and double numeric values
const transformed = transformObject(original, (key, value) => {
const newKey = key.toUpperCase();
const newValue = typeof value === 'number' ? value * 2 : value;
return [newKey, newValue];
});
console.log(transformed);
// Expected output: { NAME: "Alice", AGE: 50, OCCUPATION: "Software Engineer" }

Constraints:

  • The input object will always be a valid object.
  • The transformation function will always return an array [newKey, newValue].
  • The function should return a new object with the transformed properties. - The original object should not be modified.

window code 2Test Cases

Input:

{
  "age": 25,
  "name": "Alice",
  "occupation": "Software Engineer"
}

Expected Output:

{
  "AGE": 50,
  "NAME": "Alice",
  "OCCUPATION": "Software Engineer"
}