gpt4 book ai didi

javascript - array.splice 删除错误的值并保留错误的值

转载 作者:行者123 更新时间:2023-11-28 13:27:01 25 4
gpt4 key购买 nike

我想要一个由没有“拒绝”作为回应的客人组成的数组。因此,牙仙子被从阵列中删除(不应该,她“接受”了邀请), jack ·弗罗斯特留下来(不应该,他“拒绝”了邀请)。

function getAttendees(peopleInvited, responses){
var coming=peopleInvited;
responses.map(function(cell){
if (cell.response=='declined') {
coming.splice(0,1);
}
});
return coming;
}

var people = ['Easter Bunny', 'Tooth Fairy', 'Frosty the Snowman',
'Jack Frost', 'Cupid', 'Father Time'];
var responses = [
{name: 'Easter Bunny', response: 'declined'},
{name: 'Jack Frost', response: 'declined'},
{name: 'Tooth Fairy', response: 'accepted'}
];

getAttendees(people, responses);

最佳答案

首先,您需要获取传入数组中人员的索引,然后使用 splice 根据其索引删除该人员。

function getAttendees(peopleInvited, responses){
var coming=peopleInvited;
responses.map(function(cell){
if (cell.response=='declined') {
var index = coming.indexOf(cell.name);
coming.splice(index, 1);
}
});
return coming;
}

var people = ['Easter Bunny', 'Tooth Fairy', 'Frosty the Snowman',
'Jack Frost', 'Cupid', 'Father Time'];
var responses = [
{name: 'Easter Bunny', response: 'declined'},
{name: 'Jack Frost', response: 'declined'},
{name: 'Tooth Fairy', response: 'accepted'}
];

getAttendees(people, responses);

关于javascript - array.splice 删除错误的值并保留错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28343457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com