鉴于以下成员数据数组,我要为会员身份不是最新的会员返回formEl : Model -> E.Element MyStyles variation Msg
formEl model =
E.column None [] <|
[ Input.email FormField
[ Attr.width (Attr.px 300) ]
{ onChange = FormChange
, value = ""
, label = Input.labelAbove <| E.text "Email"
, options =
[ Input.errorBelow <|
E.el Error [] (E.text "Fill in an email address")
]
}
, Input.text FormField
[ Attr.width (Attr.px 300) ]
{ onChange = FormChange
, value = ""
, label = Input.labelAbove <| E.text "Distance"
, options =
[ Input.errorBelow <|
E.el Error [] (E.text "Fill in a distance")
]
}
-- <DATE PICKER HERE>
]
数组。
这是会员数据:
memberID
&#13;
以下是我获取该数据的代码:
var members = [
{
firstName: 'Howard',
lastName: 'Lee',
membershipIsCurrent: true,
MemberID: 235
},
{
firstName: 'James',
lastName: 'Icharis',
membershipIsCurrent: false,
MemberID: 236
},
{
firstName: 'Thomas',
lastName: 'Cronquist',
membershipIsCurrent: true,
MemberID: 237
},
{
firstName: 'Philip',
lastName: 'Grover',
membershipIsCurrent: false,
MemberID: 238
},
{
firstName: 'Eric',
lastName: 'Broadstone',
membershipIsCurrent: true,
MemberID: 239
},
{
firstName: 'Hunter',
lastName: 'Gonzales',
membershipIsCurrent: true,
MemberID: 240
}];
&#13;
结果如下。我不明白为什么它会为会员资格尚未失效的每位会员返回 function lapsedIDs (array ) {
return array.map( function ( member ) {
if ( member.membershipIsCurrent === false ) {
return member.MemberID;
}
});
}
lapsedIDs(members);
。任何指针??
undefined
&#13;
答案 0 :(得分:6)
map()
将为原始数组中的每个现有元素创建一个新元素。
如果你没有return
所有条件的东西,函数会自动返回undefined,这就是你所看到的
您可以先filter()
删除不需要的内容,然后map()
return array.map( function ( member ) {
return member.membershipIsCurrent
}).map(function(member){
return member.MemberID;
});
答案 1 :(得分:3)
为什么会出现这个问题: - 因为 .map 每次都应该返回一个值,如果条件为false,则函数返回undefined,因为你没有提供任何返回值。 该怎么办 ?首先使用过滤器,然后使用地图查看下面的解决方案
var members = [
{
firstName: 'Howard',
lastName: 'Lee',
membershipIsCurrent: true,
MemberID: 235
},
{
firstName: 'James',
lastName: 'Icharis',
membershipIsCurrent: false,
MemberID: 236
},
{
firstName: 'Thomas',
lastName: 'Cronquist',
membershipIsCurrent: true,
MemberID: 237
},
{
firstName: 'Philip',
lastName: 'Grover',
membershipIsCurrent: false,
MemberID: 238
},
{
firstName: 'Eric',
lastName: 'Broadstone',
membershipIsCurrent: true,
MemberID: 239
},
{
firstName: 'Hunter',
lastName: 'Gonzales',
membershipIsCurrent: true,
MemberID: 240
}];
function lapsedIDs (array ) {
return array.filter( function ( member ) {
return member.membershipIsCurrent === false
}).map(function(member){
return member.MemberID;
});
}
console.log(lapsedIDs(members));
&#13;
答案 2 :(得分:0)
Array.map()
在源数组中的每个元素上运行,并将其映射到其他内容。您没有从当前成员返回成员ID,因此它将它们映射到undefined
。您可能只想修改函数以使用let lapsedMembers = [];
开始,然后使用forEach
而不是map
,并在该循环中将ID推送到lapsedMembers
数组。 / p>
var members = [
{
firstName: 'Howard',
lastName: 'Lee',
membershipIsCurrent: true,
MemberID: 235
},
{
firstName: 'James',
lastName: 'Icharis',
membershipIsCurrent: false,
MemberID: 236
},
{
firstName: 'Thomas',
lastName: 'Cronquist',
membershipIsCurrent: true,
MemberID: 237
},
{
firstName: 'Philip',
lastName: 'Grover',
membershipIsCurrent: false,
MemberID: 238
},
{
firstName: 'Eric',
lastName: 'Broadstone',
membershipIsCurrent: true,
MemberID: 239
},
{
firstName: 'Hunter',
lastName: 'Gonzales',
membershipIsCurrent: true,
MemberID: 240
}];
function lapsedIDs ( array ) {
let lapsedMembers = [];
array.forEach(function ( member ) {
if ( member.membershipIsCurrent === false ) {
lapsedMembers.push(member.MemberID);
}
});
return lapsedMembers;
}
console.log(lapsedIDs(members));
答案 3 :(得分:0)
即使您的map
测试失败,membershipIsCurrent
也希望将某些放入数组中。所以它会引发undefined
。
您可以在地图之前filter
来避免这种情况。
function lapsedIDs(array) {
return array.filter(member => {
return !member.membershipIsCurrent;
}).map(member => member.MemberID);
}
lapsedIDs(members);
答案 4 :(得分:0)
我试图解决同样的问题,并在看了这篇文章后意识到最简单的解决方案是
function lapsedIDs (array ) {
return array.map( function ( member ) {
if ( member.membershipIsCurrent === false ) {
return member.MemberID;
}
}).filter(y => y !== undefined);
}
就我而言,我不知道输入是什么,我也不在乎,我只希望地图返回所有定义的值。