【发布时间】:2021-02-28 12:00:47
【问题描述】:
我在 Web 应用程序中有一个电影对象数组,每个对象都有 title(string)、release year(int)、rating(int) 和 genre(an array of string e.g ['Action','Sci-Fi] 的属性,现在一些电影对象有 format(string) 的属性而其他人则没有。我所需要的只是帮助遍历这些对象并检查哪些对象没有format 属性并将该属性添加到这些对象并使用Film 为那些没有的对象设置值.. .
这是我到目前为止尝试的js代码
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
}
感谢您的意见。
【问题讨论】:
-
让我先处理它
-
我需要在我的代码上实现的解决方案
标签: javascript loops iteration