I'm trying to solve this problem:
In this function you will be provided with an object. That object is storing information on keys. E.g.
{
name: 'Tom',
job: ['writing katas', 'marking'],
favouriteShop: [
"Paul's Donkey University",
"Shaq's Taxidermy Shack",
"Sam's Pet Shop"
]
};
In some cases, however, the keys have been very badly named. Good naming convention tells us that the keys containing arrays should be named as plurals.
This function should return a new object that is a copy of the input but with any keys that contain arrays pluralised (an 's' added to the end.) E.g.
{
name: 'Tom',
jobs: ['writing katas', 'marking'],
favouriteShops: [
"Paul's Donkey University",
"Shaq's Taxidermy Shack",
"Sam's Pet Shop"
]
}
My attempt:
let cloneObj = Object.assign({},obj);
let arrayOfKeys = Object.keys(obj);
if(arrayOfKeys.length === 3){
let plural = arrayOfKeys[1]+"s"
cloneObj[plural] = cloneObj[arrayOfKeys[1]];
delete cloneObj[arrayOfKeys[1]];
}
return cloneObj;
Here I'm always pointing the 2nd index key position of object. But here this is not the case the key with array value is placed randomly in the object.