Validate whether all Object.keys() of the parameter are equal to the specific keys (JavaScript)
I am new here. I'm learning web programming, and now I'm a bit stuck with Javascript. I want to validate that, when a new object is added to an array, it has all the required keys. I have shared below an array object, an example object with all the keys, and an object that does not have all the required keys. I tried to use Object.keys() to compare the keys of an array object, with the object to be input. However, unfortunately both objects that have all the keys and objects that do not have all the keys are taken as incomplete. I cannot add any new objects, even though they have all the keys that the other objects in the array have. Does anyone know where I am going wrong? I would be very grateful for help.
Thanks and best regards to all!
Francisco
Code:
let pokemonRepository = (function () {
...
pokedex = [
{
name: 'Blastoise',
types: ['Water', 'None'],
height: 1.6,
weight: 85.5,
gender: ['Male', 'Female'],
category: 'Shellfish',
evolutions: ['None'],
description:
'It crushes its foe under its heavy body to cause fainting. In a pinch, it will withdraw inside its shell.',
},
];
function add(item) {
if (typeof item === 'object') {
// This is where I am failing
if (Object.keys(item) === Object.keys(pokemonList[0])) {
alert(
`You have discovered a new Pokémon! "${item.name}" data has been entered into the Pokédex.`
);
pokemonList.push(item);
} else {
alert(
`The data for the new Pokémon you are trying to add is not complete. Please verify that no fields are missing.`
);
}
} else {
alert(
`"${item}" is not a valid Pokémon! Please check that the data type typed in is an object.`
);
}
}
...
})();
// Add a valid object
pokemonRepository.add({
name: 'Wartortle',
types: ['Water', 'None'],
height: 1,
weight: 22.5,
gender: ['Male', 'Female'],
category: 'Turtle',
evolutions: ['Blastoise'],
description:
'It is recognized as a symbol of longevity. If its shell has algae on it, that Wartortle is very old.',
});
// Add a invalid object (missing data)
pokemonRepository.add({
name: 'Bla',
types: ['Bla', 'Bla'],
height: 1,
weight: 800,
});
Edit:
Thank you very much to everyone who answered me! I never thought you would see my message. I am very grateful to you. After trying for more than 2 hours to find the solution, I have managed to correct the problem thanks to your help. Thank you all! The code looks like this (I only changed the way of comparing the keys of the objects):
function add(item) {
if (typeof item === 'object') {
// Changes here
if (Object.keys(pokemonList[0]).every((key) => key in item)) {
// Changes here
alert(
`You have discovered a new Pokémon! "${item.name}" data has been entered into the Pokédex.`
);
// Changes here (to validate)
console.log(Object.keys(pokemonList[0]).every((key) => key in item));
// Changes here (to validate)
pokemonList.push(item);
} else {
alert(
`The data for the new Pokémon you are trying to add is not complete. Please verify that no fields are missing.`
);
// Changes here (to validate)
console.log(Object.keys(pokemonList[0]).every((key) => key in item));
// Changes here (to validate)
}
} else {
alert(
`"${item}" is not a valid Pokémon! Please check that the data type typed in is an object.`
);
}
}
Thanks to all of you wonderful people of the internet! Francisco