Sobes.tech
Junior — Middle+

Extraction of selected read-only object fields

livecode

Task condition

It is necessary to write a function getObjectValues that takes two arguments: the source object and an array of string keys. The function should create and return a new object that contains only the properties whose names are specified in the array, along with their values. Additionally, the properties of the resulting object should be protected from modification – any attempts to assign a new value should result in an access error.

const user = {
  name: 'dima',
  age: 24,
  channel: 'SIBERIA CAN CODE - Frontend',
};

const nameAndChannel = getObjectValues(user, ['name', 'channel']);
console.log('@', nameAndChannel);

nameAndChannel.name = 'ne dima'; // error readonly

In the solution, you can use methods like Object.defineProperty, Object.defineProperties, or Object.freeze to ensure immutability of the properties.