-3

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.

  • Does this answer your question? [How do I check if an object has a specific property in JavaScript?](https://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-specific-property-in-javascript) – pilchard Feb 28 '21 at 12:04
  • lemme process it first –  Feb 28 '21 at 12:08
  • I need a solution implemented on my code –  Feb 28 '21 at 12:10

1 Answers1

0

I am new to JavaScript but hopefully that works.

function myfunction(item, index) {
  if (!item.hasOwnProperty("format")) {
    item.format = "Film";
  }
}
pilchard
  • 12,414
  • 5
  • 11
  • 23
Priyanshu Jangra
  • 169
  • 2
  • 13
  • I actually did it for complete movies. I am really sorry for not reading carefully. – Priyanshu Jangra Feb 28 '21 at 12:19
  • It didt, says that ```format``` in the loop is undefined, should i quote it? –  Feb 28 '21 at 12:23
  • Yes, I'm sorry again. You need to quote it. – Priyanshu Jangra Feb 28 '21 at 12:25
  • its returning no output, should i expect it to make changes to the structure of the objects without format property? –  Feb 28 '21 at 12:36
  • It is adding objects property format with value "Film" to all those who don't have format property as you asked. Nothing is being returned by the function. And if it is showing no error, it is probably working fine. – Priyanshu Jangra Feb 28 '21 at 12:39
  • Thats why i asked if i should expect to see those objects which didnt have property to have after i run code? –  Feb 28 '21 at 12:42