如果js中有两个或更多相等的值,我需要返回一个工作日数组

lvmkulzt  于 2021-09-13  发布在  Java
关注(0)|答案(3)|浏览(380)

解释有点难,但以下是官方解释:
mostpopulardays:此函数确定一周中哪一天访问宠物店的人数最多。如果两天或两天以上的通信量最高,则应返回包含这些天的数组(以任何顺序)。如果输入为null或空数组,则函数应返回null。输入是一个工作日对象数组,这些对象是使用petstore.js底部定义的原型函数创建的。如果只有一个最流行的日子,此函数应返回一个字符串,其中包含一周中最流行的日子的名称;如果有多个最流行的日子,此函数应返回一个数组,其中包含最流行的日子的名称(作为字符串)。
所以我现在的代码是:

function mostPopularDays (week) {
  var name,
      dayInstance,
      highestTrafficYet = -1;

  for (var i = 0; i < week.length; ++i) {
    dayInstance = week[i];
    traffic = dayInstance.traffic;

    if (highestTrafficYet < traffic) {
      name = dayInstance.name;
      highestTrafficYet = traffic;
    }
  }
  return name;
}

如果存在多个相等值,如何返回数组

2uluyalo

2uluyalo1#

这将有助于:

function mostPopularDays(week) {
  var name,
    dayInstance,
    highestTrafficYet = -1,
    trafficIndexedDaysObj = {};

  for (var i = 0; i < week.length; ++i) {
    dayInstance = week[i];
    traffic = dayInstance.traffic;

    if (highestTrafficYet < traffic) {
      name = dayInstance.name;
      highestTrafficYet = traffic;

      // Clear previous hightest days
      trafficIndexedDaysObj = {};

      // Store highest Day in Array and put the Array in an Object with Traffic as key
      trafficIndexedDaysObj[traffic] = [name];
    } else if (highestTrafficYet == traffic && Array.isArray(trafficIndexedDaysObj[traffic])) {
      // If the same Traffic exists, append that Day to the already created Days Array
      trafficIndexedDaysObj[traffic].push(dayInstance.name);
    }
  }

  if (Object.keys(trafficIndexedDaysObj).length > 0) {
    // Return array of Days when the Highest Traffic occurred on more than one Days 
    var keys = Object.keys(trafficIndexedDaysObj);

    if (trafficIndexedDaysObj[keys[0]].length > 1) {
      return trafficIndexedDaysObj[keys[0]];
    }
  }

  return name;
}

mostPopularDays([{
  'traffic': 100,
  'name': 'Monday'
}, {
  'traffic': 64,
  'name': 'Tuesday'
}, {
  'traffic': 52,
  'name': 'Wednesday'
}, {
  'traffic': 100,
  'name': 'Thursday'
}, {
  'traffic': 100,
  'name': 'Friday'
}, {
  'traffic': 100,
  'name': 'Saturday'
}])

https://jsfiddle.net/praveen_tamil/ync47bjv/18/

3gtaxfhh

3gtaxfhh2#

您可以先找到访问人数最多的人,然后使用 Array#filter 去寻找那些有那么多人的日子。

function mostPopularDays (week) {
  if(!week?.length) return null;
  const max = Math.max(...week.map(x => x.traffic));
  const days = week.filter(x => x.traffic === max).map(x => x.name);
  return days.length === 1 ? days[0]: days;
}
cidc1ykv

cidc1ykv3#

有关解释,请参见下面的注解,但如果您首先对值进行排序,则可以找到最高流量日(或其中一个)。然后使用该流量数测试其余的流量,可以过滤掉所有没有那么高的流量。最后,如果只有一个最高的日期,可以输出一个名称;如果有多个最高的日期,则可以将结果Map到name属性。

let data = [{
  traffic: 4, name: 'mon'
}, {
  traffic: 2, name: 'weds'
}, {
  traffic: 4, name: 'tues'
}];

function mostPopularDays(week) {
  let sorted = week.sort((a, b) => b.traffic - a.traffic); // get the higehest traffic days at the top
  let highest = sorted[0].traffic // sample the highest value
  days = sorted.filter(a => a.traffic >= highest); // filter the days to only have the highest
  if (days.length === 1) return days[0].name // just one? return the name
  else return days.map(e => e.name) // more than one? map the array to be just the day names
}
console.log(mostPopularDays(data))

相关问题