我正在使用以下Lodash链接的实用程序来映射/平面和数组并生成一个新的,同时排除未定义的值。
SELECT AVG(outertable.dis) AS disaverage, outertable.yr,
outertable.pitcher AS pitcher,
outertable.pitch_type AS pitch_type, outertable.war, outertable.gs,
outertable.temp, rsgames2.ATTEND_PARK_CT, rsgames2.DAYNIGHT_PARK_CD,
rsgames2.WIND_SPEED_PARK_CT, teamdata.LNPop, teamdata.PPMult,
teamdata.FCI
FROM
( SELECT distinct sqrt(((abs((power(pitches.px- myavgtable.apx, 2)) +
power(pitches.pz-myavgtable.apz,2))))) AS dis, myavgtable.yr,
myavgtable.pitcher, myavgtable.pitch_type, myavgtable.war,
myavgtable.gs, myavgtable.temp, myavgtable.game_id, myavgtable.HOME
FROM
( SELECT distinct atbats.pitcher AS pitcher, YEAR(games2.DATE) AS yr,
pitches.pitch_type AS pitch_type, pitches.pitch_type AS pt,
AVG(pitches.px) AS apx,
AVG(pitches.pz) AS apz,
pitcherwar.WAR as war,
pitcherwar.GS as gs,
games2.temp as temp,
games2.game_id AS game_id, games2.HOME as HOME
from atbats
join pitches on pitches.ab_id = atbats.ab_id
join pitcherwar on atbats.pitcher = pitcherwar.eliasid
join games2 on atbats.game_id = games2.game_id
where atbats.pitcher = games2.home_starting_pitcher
AND atbats.pitcher = pitcherwar.eliasid
AND pitches.pitch_type != 'IN'
AND pitches.pitch_type != 'PO'
AND pitches.pitch_type != 'UN'
AND YEAR(games2.DATE)>=2008
group by atbats.pitcher, pitches.pitch_type, yr
) AS myavgtable
JOIN atbats ON atbats.pitcher=myavgtable.pitcher
JOIN pitches ON atbats.ab_id=pitches.ab_id
JOIN games2 on atbats.game_id=games2.game_id
) AS outertable
JOIN rsgames2 on outertable.game_id = rsgames2.game_id
JOIN teamdata on outertable.HOME = teamdata.Team
and outertable.yr = teamdata.Year
WHERE outertable.dis IS NOT NULL
GROUP BY outertable.pitcher, outertable.pitch_type, outertable.yr;
这就是结果:
如您所见,仍然包含未定义的值。这是为什么?
修改
resp.data看起来像这样
const array = _(resp.data)
.omit(_.isUndefined)
.flatMap('building')
.value()
console.log(array)
EDIT2:
这些[
{ username: '', building: [ name: '' ] }
{ username: '', building: [ name: '' ] }
// etc...
]
值可能是对象所拥有的空undefined
。
答案 0 :(得分:2)
额外undefined
值应属于user
中resp.data
,其中不存在building
。您要做的就是_.filter()
undefined
之后_.identity()
到_.flatMap()
的所有值_.omit
。
注意:使用_.flatMap()
只能用于对象,在数组的上下文中使用它(例如_.filter()
导致数组)将产生一个对象的结果,其中索引为每个项目都是数组中的索引。您应该使用var data = [
{ username: 'user1', building: [ { name: 'building1' } ] },
{ username: 'user2', building: [ { name: 'building2' } ] },
{ username: 'user3' }
];
var result = _(data)
.flatMap('building')
.filter(_.identity)
.value();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
代替。
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>
&#13;
{{1}}&#13;
答案 1 :(得分:1)
试试这个。
const array = _(resp.data)
.omit(_.isUndefined)
.flatMap('building')
.map()
.omit(_.isUndefined)
.value()