Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Check if an object contains a property called format in Javascript without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I have an array of movie objects in a web application, and each object has properties for title(string)
,release year(int)
,rating(int)
and genre(an array of string e.g ['Action','Sci-Fi]
,now some of the movie objects got a property for format(string)
while others don’t.All I need is help iterating through these objects and checking which objects don’t have a format
property and add the property to these objects and set the values with Film
for those that do not…
Here is the js code i tried so far
var fractured={ title:"Fractured", release:2019, rating:8, format:"digital", genre:[ "Mystery","Sci-Fi","Western"] }; var countdown={ title:"Countdown", release:2018, rating:5, genre:["Sci-Fi","Mystery","Western"] } var bloodshot={ title:"Bloodshot", release:2020, rating:6, format:"digital", genre:["Sci-Fi","Action"] } var revenant={ title:"Revenant", release:2015, rating:3, genre:["History","Western","Action"] } var crisis={ title:"Crisis", release:2016, rating:10, genre:["Action","Drama","Reality"] } var life={ title:"Life", release:2017, rating:9, format:"digital", genre:["Sci-Fi","Action","Mystery"] } var nmovies=new Array(fractured,life,crisis,revenant,bloodshot,countdown); //This line outputs the title property of the first object in nmovies; console.log(nmovies[0].title); //this is the iteration that needs to check if an object has a format property and add it with the value 'Film' nmovies.forEach(myfunction()); function myfunction(item, index){ //Code to check please }
Thank you for your input.
Answer
I am new to JavaScript but hopefully that works.
function myfunction(item, index) { if (!item.hasOwnProperty("format")) { item.format = "Film"; } }
We are here to answer your question about Check if an object contains a property called format in Javascript - If you find the proper solution, please don't forgot to share this with your team members.