遍历对象数组,并根据在数组中出现的次数选择对象

  • 本文关键字:对象 数组 选择 遍历 javascript
  • 更新时间 :
  • 英文 :


const bondFilms = [
{ "title" : "Skyfall", "year" : 2012, "actor" : "Daniel Craig", "gross" : "$1,108,561,008" },
{ "title" : "Thunderball", "year" : 1965, "actor" : "Sean Connery", "gross" : "$1,014,941,117" },
{ "title" : "Goldfinger", "year" : 1964, "actor" : "Sean Connery", "gross" : "$912,257,512" },
{ "title" : "Live and Let Die", "year" : 1973, "actor" : "Roger Moore", "gross" : "$825,110,761" },
{ "title" : "You Only Live Twice", "year" : 1967, "actor" : "Sean Connery", "gross" : "$756,544,419" },
{ "title" : "The Spy Who Loved Me", "year" : 1977, "actor" : "Roger Moore", "gross" : "$692,713,752" },
{ "title" : "Casino Royale", "year" : 2006, "actor" : "Daniel Craig", "gross" : "$669,789,482" },
{ "title" : "Moonraker", "year" : 1979, "actor" : "Roger Moore", "gross" : "$655,872,400" },
{ "title" : "Diamonds Are Forever", "year" : 1971, "actor" : "Sean Connery", "gross" : "$648,514,469" },
{ "title" : "Quantum of Solace", "year" : 2008, "actor" : "Daniel Craig", "gross" : "$622,246,378" },
{ "title" : "From Russia with Love", "year" : 1963, "actor" : "Sean Connery", "gross" : "$576,277,964" },
{ "title" : "Die Another Day", "year" : 2002, "actor" : "Pierce Brosnan", "gross" : "$543,639,638" },
{ "title" : "Goldeneye", "year" : 1995, "actor" : "Pierce Brosnan", "gross" : "$529,548,711" },
{ "title" : "On Her Majesty's Secret Service", "year" : 1969, "actor" : "George Lazenby", "gross" : "$505,899,782" },
{ "title" : "The World is Not Enough", "year" : 1999, "actor" : "Pierce Brosnan", "gross" : "$491,617,153" },
{ "title" : "For Your Eyes Only", "year" : 1981, "actor" : "Roger Moore", "gross" : "$486,468,881" },
{ "title" : "Tomorrow Never Dies", "year" : 1997, "actor" : "Pierce Brosnan", "gross" : "$478,946,402" },
{ "title" : "The Man with the Golden Gun", "year" : 1974, "actor" : "Roger Moore", "gross" : "$448,249,281" },
{ "title" : "Dr. No", "year" : 1962, "actor" : "Sean Connery", "gross" : "$440,759,072" },
{ "title" : "Octopussy", "year" : 1983, "actor" : "Roger Moore", "gross" : "$426,244,352" },
{ "title" : "The Living Daylights", "year" : 1987, "actor" : "Timothy Dalton", "gross" : "$381,088,866" },
{ "title" : "A View to a Kill", "year" : 1985, "actor" : "Roger Moore", "gross" : "$321,172,633" },
{ "title" : "License to Kill", "year" : 1989, "actor" : "Timothy Dalton", "gross" : "$285,157,191" }
];
const leastBondAppearance = []
for (let i = 0; i < bondFilms.length; i++){
leastBondAppearance.push(bondFilms[i].actor)
}
// console.log(leastBondAppearance)
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(leastBondAppearance))

这给了我一个字符串数组,其中包含所有重复的演员名称。我不知道如何计算这些字符串出现了多少次。然后,我需要返回一个对象,其中包含出现bond电影次数最少的演员。

我知道我还需要写一些逻辑来说明哪个演员出现的次数最少,但在此之前我丢失了几个步骤。

下面是最终的结果。

{ 
"title" : "On Her Majesty's Secret Service", 
"year" : 1969, 
"actor" : "George Lazenby", 
"gross" : "$505,899,782" 
}

那是因为乔治·拉赞比只演过一部邦德电影。

let actors = {}
bondFilms.forEach(f => {
if (!actors[f.actor]) actors[f.actor] = 0
actors[f.actor] = actors[f.actor]+1 // or actors[f.actor]++
})
let sortedActorNames = Object.keys(actors).sort((a,b) => actors[a]-actors[b])
console.log(sortedActorNames[0])

您可以首先获得所有唯一的参与者,然后至少使用嵌套的for循环和计数器变量检查其中哪些出现。

const uniqueBonds = [];
let res ;
let counter = 0;
let max = 9007199254740991;

bondFilms.forEach((x) => {
if (!uniqueBonds.includes(x.actor.trim())) {
uniqueBonds.push(x.actor)
}
})

uniqueBonds.forEach((x) => {
let temp;
bondFilms.forEach((y) => {
if (x === y.actor.trim()) {
counter += 1;
temp = y;
}
})
if (counter < max) {
max = counter;
res = temp;
}
counter = 0;
})

const bondFilms = [{
"title": "Skyfall",
"year": 2012,
"actor": "Daniel Craig",
"gross": "$1,108,561,008"
},
{
"title": "Thunderball",
"year": 1965,
"actor": "Sean Connery",
"gross": "$1,014,941,117"
},
{
"title": "Goldfinger",
"year": 1964,
"actor": "Sean Connery",
"gross": "$912,257,512"
},
{
"title": "Live and Let Die",
"year": 1973,
"actor": "Roger Moore",
"gross": "$825,110,761"
},
{
"title": "You Only Live Twice",
"year": 1967,
"actor": "Sean Connery",
"gross": "$756,544,419"
},
{
"title": "The Spy Who Loved Me",
"year": 1977,
"actor": "Roger Moore",
"gross": "$692,713,752"
},
{
"title": "Casino Royale",
"year": 2006,
"actor": "Daniel Craig",
"gross": "$669,789,482"
},
{
"title": "Moonraker",
"year": 1979,
"actor": "Roger Moore",
"gross": "$655,872,400"
},
{
"title": "Diamonds Are Forever",
"year": 1971,
"actor": "Sean Connery",
"gross": "$648,514,469"
},
{
"title": "Quantum of Solace",
"year": 2008,
"actor": "Daniel Craig",
"gross": "$622,246,378"
},
{
"title": "From Russia with Love",
"year": 1963,
"actor": "Sean Connery",
"gross": "$576,277,964"
},
{
"title": "Die Another Day",
"year": 2002,
"actor": "Pierce Brosnan",
"gross": "$543,639,638"
},
{
"title": "Goldeneye",
"year": 1995,
"actor": "Pierce Brosnan",
"gross": "$529,548,711"
},
{
"title": "On Her Majesty's Secret Service",
"year": 1969,
"actor": "George Lazenby",
"gross": "$505,899,782"
},
{
"title": "The World is Not Enough",
"year": 1999,
"actor": "Pierce Brosnan",
"gross": "$491,617,153"
},
{
"title": "For Your Eyes Only",
"year": 1981,
"actor": "Roger Moore",
"gross": "$486,468,881"
},
{
"title": "Tomorrow Never Dies",
"year": 1997,
"actor": "Pierce Brosnan",
"gross": "$478,946,402"
},
{
"title": "The Man with the Golden Gun",
"year": 1974,
"actor": "Roger Moore",
"gross": "$448,249,281"
},
{
"title": "Dr. No",
"year": 1962,
"actor": "Sean Connery",
"gross": "$440,759,072"
},
{
"title": "Octopussy",
"year": 1983,
"actor": "Roger Moore",
"gross": "$426,244,352"
},
{
"title": "The Living Daylights",
"year": 1987,
"actor": "Timothy Dalton",
"gross": "$381,088,866"
},
{
"title": "A View to a Kill",
"year": 1985,
"actor": "Roger Moore",
"gross": "$321,172,633"
},
{
"title": "License to Kill",
"year": 1989,
"actor": "Timothy Dalton",
"gross": "$285,157,191"
}
];

const uniqueBonds = [];
let res ;
let counter = 0;
let max = 9007199254740991;

bondFilms.forEach((x) => {
if (!uniqueBonds.includes(x.actor.trim())) {
uniqueBonds.push(x.actor)
}
})

uniqueBonds.forEach((x) => {
let temp;
bondFilms.forEach((y) => {
if (x === y.actor.trim()) {
counter += 1;
temp = y;
}
})
if (counter < max) {
max = counter;
res = temp;
}
counter = 0;
})
console.log(res)

最新更新