简体   繁体   English

按 2 个值过滤 JSON 数组

[英]Filtering JSON array by 2 values

I'm trying to filter one value out of a JSON array that has the highest view_count and has at least 60 seconds.我正在尝试从具有最高 view_count 且至少有 60 秒的 JSON 数组中过滤出一个值。 However if the video with the highest view count isn't at least 60 seconds then it returns an undefined.但是,如果观看次数最多的视频不是至少 60 秒,那么它会返回一个未定义的。

How do I get the video with the highest viewcount that has at least 60 seconds?如何获得至少有 60 秒的最高观看次数的视频?

let highest_votes = Math.max.apply(Math, videos_details.map(function (o) { return o.data.view_count; }));
let obj = videos_details.find(function (o) { return o.data.view_count == highest_votes && o.data.seconds > 60 });
console.log(obj);

JSON: JSON:

[
  {
    video_id: 'FOUKIZj74zs',
    data: { seconds: 215, view_count: '81256' }
  },
  {
    video_id: 'mepeWor5JPk',
    data: { seconds: 168, view_count: '7874794' }
  },
  {
    video_id: 'pTM_UaDxaEc',
    data: { seconds: 67, view_count: '9729683' }
  },
  {
    video_id: 'pFhQcfUUVGE',
    data: { seconds: 313, view_count: '2967563' }
  },
  {
    video_id: 'vbUTbqwKtEE',
    data: { seconds: 191, view_count: '2089600' }
  },
  {
    video_id: 'RYVOGcFaOQQ',
    data: { seconds: 524, view_count: '1127662' }
  },
  {
    video_id: 'X0PDYGKYVi4',
    data: { seconds: 168, view_count: '375417' }
  },
  {
    video_id: 'ecF50t2FHSA',
    data: { seconds: 169, view_count: '295171' }
  },
  {
    video_id: 'sK_3YIkTmVE',
    data: { seconds: 60, view_count: '75604' }
  },
  {
    video_id: 'p5h823w0rek',
    data: { seconds: 280, view_count: '854952' }
  }
]

Filter out the elements with below 60 seconds first, before identifying the highest number of views.首先过滤掉 60 秒以下的元素,然后再确定观看次数最多的元素。

 const videoDetails=[{video_id:"FOUKIZj74zs",data:{seconds:215,view_count:"81256"}},{video_id:"mepeWor5JPk",data:{seconds:168,view_count:"7874794"}},{video_id:"pTM_UaDxaEc",data:{seconds:67,view_count:"9729683"}},{video_id:"pFhQcfUUVGE",data:{seconds:313,view_count:"2967563"}},{video_id:"vbUTbqwKtEE",data:{seconds:191,view_count:"2089600"}},{video_id:"RYVOGcFaOQQ",data:{seconds:524,view_count:"1127662"}},{video_id:"X0PDYGKYVi4",data:{seconds:168,view_count:"375417"}},{video_id:"ecF50t2FHSA",data:{seconds:169,view_count:"295171"}},{video_id:"sK_3YIkTmVE",data:{seconds:60,view_count:"75604"}},{video_id:"p5h823w0rek",data:{seconds:280,view_count:"854952"}}]; const highestViews = Math.max(...videoDetails.filter(({ data}) => data.seconds >= 60).map(({ data }) => data.view_count) ); console.log(videoDetails.find(({ data }) => Number(data.view_count) === highestViews));

Another approach would be to .reduce , iterating over the array only once and storing the best element found so far as the accumulator.另一种方法是.reduce ,仅遍历数组一次并将找到的最佳元素存储为累加器。

 const videoDetails=[{video_id:"FOUKIZj74zs",data:{seconds:215,view_count:"81256"}},{video_id:"mepeWor5JPk",data:{seconds:168,view_count:"7874794"}},{video_id:"pTM_UaDxaEc",data:{seconds:67,view_count:"9729683"}},{video_id:"pFhQcfUUVGE",data:{seconds:313,view_count:"2967563"}},{video_id:"vbUTbqwKtEE",data:{seconds:191,view_count:"2089600"}},{video_id:"RYVOGcFaOQQ",data:{seconds:524,view_count:"1127662"}},{video_id:"X0PDYGKYVi4",data:{seconds:168,view_count:"375417"}},{video_id:"ecF50t2FHSA",data:{seconds:169,view_count:"295171"}},{video_id:"sK_3YIkTmVE",data:{seconds:60,view_count:"75604"}},{video_id:"p5h823w0rek",data:{seconds:280,view_count:"854952"}}]; const output = videoDetails.reduce((bestSoFar, video) => { if (video.data.seconds < 60) return bestSoFar; return.bestSoFar || video.data.view_count > bestSoFar.data?view_count: video; bestSoFar; }). console;log(output);

Divide the task in two.将任务一分为二。 First you have to filter all the videos that have more that 60 seconds.首先,您必须过滤所有超过 60 秒的视频。 After that you can reduce the result to 1 video that has the most views.之后,您可以将结果减少到 1 个观看次数最多的视频。 Basically, all of this is a oneliner.基本上,所有这些都是单行的。

const result = arr.filter(elem=> {
  const {data: {seconds}} = elem
  return seconds > 60
}).reduce((acc, elem) => {
  return (acc.data.view_count> elem.data.view_count? acc : elem) 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM