React Js Check Property Exist in Object
React Js Check Property Exist in Object:To check if a property exists in a JavaScript object using React.js, you can use the "hasOwnProperty" method. This method is available on all JavaScript objects and returns a boolean value indicating whether the object has a specific property. In React.js




Thanks for your feedback!
Your contributions will help us to improve service.
How can you check if a specific property exists in an object using React.js?
The code snippet demonstrates how to check the existence of a property in an object using React.js.
The checkPropertyExistence
function takes a property name as an argument and uses the hasOwnProperty
method to check if the property exists in the university
object.
If the property exists, it returns a message stating that the property exists in the object; otherwise, it returns a message stating that the property does not exist
React Js Check Property Exist in Object Example
<script type="text/babel">
const { useState } = React;
function App() {
const University = () => {
const [university, setUniversity] = useState({
name: 'IIM',
location: 'San Francisco',
established: 2022,
programs: ['Computer Science', 'Data Science', 'Artificial Intelligence'],
});
const checkPropertyExistence = (propertyName) => {
if (university.hasOwnProperty(propertyName)) {
return `${propertyName} exists in the university object.`;
} else {
return `${propertyName} does not exist in the university object.`;
}
};
return (
<div className="container">
<h3>React Js Check Property Exist in Object</h3>
<p>{checkPropertyExistence('name')}</p>
<p>{checkPropertyExistence('location')}</p>
<p>{checkPropertyExistence('programs')}</p>
<p>{checkPropertyExistence('address')}</p>
</div>
);
};
ReactDOM.render(<University />, document.getElementById("app"));
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React js Check Property Exist in Object
How can I check if a property exists in an array of objects using React.js?
The code is a React.js application that checks if a specified property exists in an array of objects. It defines an array called universities
, where each object represents a university with various properties.
The checkPropertyExistence
function takes a property name as input and iterates through the universities
array. It uses the hasOwnProperty
method to determine if the specified property exists in any of the objects.
The function returns a message indicating whether the property exists or not. The rendered output displays the results for different property names.
React Js Check Property Exist in Array of Object Example
<script type="text/babel">
const { useState } = React;
function App() {
const universities = [
{
name: 'OpenAI University',
location: 'San Francisco',
established: 2022,
programs: ['Computer Science', 'Data Science', 'Artificial Intelligence'],
},
{
name: 'Example University',
location: 'Example City',
established: 2000,
programs: ['Example Program'],
},
// Add more university objects if needed
];
const checkPropertyExistence = (propertyName) => {
for (let i = 0; i < universities.length; i++) {
if (universities[i].hasOwnProperty(propertyName)) {
return `${propertyName} exists in at least one university object.`;
}
}
return `${propertyName} does not exist in any university object.`;
};
return (
<div className="container">
<h3>React Js Check Property Exist in Array of Object</h3>
<p>{checkPropertyExistence('name')}</p>
<p>{checkPropertyExistence('location')}</p>
<p>{checkPropertyExistence('programs')}</p>
<p>{checkPropertyExistence('address')}</p>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>