Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在对象映射中插入条件语句?_Javascript - Fatal编程技术网

Javascript 如何在对象映射中插入条件语句?

Javascript 如何在对象映射中插入条件语句?,javascript,Javascript,就这一问题采取后续行动: 如果当前迭代等于一年,我想插入条件语句 我试着这样做: var monthlyHighest = Array.from( getMonthlyValues.reduce( (m, {month, subs, year}) => { if(year == 2018){ //here is the conditional statement return m.set(month, Math.max(m.get(month

就这一问题采取后续行动:

如果当前迭代等于一年,我想插入条件语句

我试着这样做:

var monthlyHighest = Array.from(
    getMonthlyValues.reduce(
     (m, {month, subs, year}) => {
      if(year == 2018){ //here is the conditional statement
        return m.set(month, Math.max(m.get(month) || 0, subs))
      }
     },new Map),
     ([month, subs, year]) => ({ month, subs, year}));
但是我得到一个“无法将未定义或null转换为object”错误,据我所知,如果行if(year==2018)返回false,整个reduce函数将不再继续

我使用以下阵列进行测试:

1: {month: "2018-07-24", subs: 2}
2: {month: "2018-07-31", subs: 3}
3: {month: "2019-08-01", subs: 2}
4: {month: "2019-08-02", subs: 3}
5: {month: "2019-08-05", subs: 3}
6: {month: "2019-08-08", subs: 4}
7: {month: "2019-08-14", subs: 5}
8: {month: "2019-08-20", subs: 7}
9: {month: "2019-08-23", subs: 7}
10: {month: "2019-08-28", subs: 8}
11: {month: "2019-08-29", subs: 11}
12: {month: "2019-09-02", subs: 2}
13: {month: "2019-09-03", subs: 2}
14: {month: "2019-09-04", subs: 3}
15: {month: "2019-09-05", subs: 5}
16: {month: "2019-09-06", subs: 5}
17: {month: "2019-09-09", subs: 6}
18: {month: "2019-09-10", subs: 7}
19: {month: "2019-09-11", subs: 8}
20: {month: "2019-09-12", subs: 9}

我认为您实际使用的数据具有
{year,month,subs}
的对象,其中month就是month

这是有效的:请注意中的硬编码年份

([month, subs, year]) => ({ month, subs, year:2018})
因为一个映射在
数组使用的iterable中只有2个元素。from
-不确定您预期的年份来自何处

const getMonthlyValue=[,,
{月份:“2018-07-24”,副标题:2},
{月份:“2018-07-31”,潜艇:3},
{月份:“2019-08-01”,潜艇:2},
{月份:“2019-08-02”,潜艇:3},
{月份:“2019-08-05”,潜艇:3},
{月份:“2019-08-08”,潜艇:4},
{月:“2019-08-14”,潜艇:5},
{月份:“2019-08-20”,潜艇:7},
{月份:“2019-08-23”,潜艇:7},
{月份:“2019-08-28”,潜艇:8},
{月份:“2019-08-29”,潜艇:11},
{月份:“2019-09-02”,潜艇:2},
{月份:“2019-09-03”,潜艇:2},
{月份:“2019-09-04”,潜艇:3},
{月份:“2019-09-05”,潜艇:5},
{月份:“2019-09-06”,潜艇:5},
{月份:“2019-09-09”,潜艇:6},
{月份:“2019-09-10”,潜艇:7},
{月份:“2019-09-11”,潜艇:8},
{月份:“2019-09-12”,潜艇:9},
].map(v=>({year:v.month.split('-')[0],month:v.month.split('-')[1],subs:v.subs}));
var monthlyHighest=Array.from(
getMonthlyValue.reduce((m,{月,子,年})=>{
如果(年份==2018){//这里是条件语句
m、 集合(月,数学最大值(m.get(月)| | 0,subs))
}
返回m;
},新地图),
([month,subs,year])=>({month,subs,year:2018})
);

console.log(月最大值)
因为当条件为false时,您不返回任何内容,因此在下一次迭代中,
m
变为
undefined
——即使条件为true,您也会返回不是
Map
m.set
的结果,然后下一次迭代,
m.set
将失败。不要
返回m.set…
。。。把
返回m
放在
条件之后
不确定你为什么认为
([month,subs,year])=>({month,subs,year})
会起作用,也就是说不会有一年谢谢@JaromandaX,我真傻。我没想到。我已经发布了一个答案。实际上map.set()返回地图,这样您就可以返回
m.set
。。。但是如果条件为false,您仍然需要
返回m