gpt4 book ai didi

arrays - 如何从用户已选择城市键的数组中获取结果,如果用户选择了城市,这些结果也将来自城市模型中未选择的城市

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:25 26 4
gpt4 key购买 nike

城市模型

[
{
"_id": '1',
"city":'A',
},
{
"_id": '2',
"city":'B',
},
{
"_id": '3',
"city":'C',
},
{
"_id": '4',
"city":'D',
},
{
"_id": '5',
"city":'E',
}
]

用户模型

[
{
"_id": '1',
"user":'sams',
"selected_city":['1','2']
}
]

想要得到结果

[

{

  "_id": "1",

  "user": "sams",

"selected_city": [
"1",
"2"
],

"not_selected": [
"3",
"4",
"5"
]
 
}
]

最佳答案

尝试:

const cities = [
{
"_id": '1',
"city":'A',
},
{
"_id": '2',
"city":'B',
},
{
"_id": '3',
"city":'C',
},
{
"_id": '4',
"city":'D',
},
{
"_id": '5',
"city":'E',
}
];


const users = [
{
"_id": '1',
"user":'sams',
"selected_city":['1','2']
}
];

const result = users.map(user => {
user.not_selected = cities
.filter(city => !user.selected_city.includes(city._id))
.map(city => city._id)
return user;
});

console.log(result);

关于arrays - 如何从用户已选择城市键的数组中获取结果,如果用户选择了城市,这些结果也将来自城市模型中未选择的城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58552981/

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