如何使用get函数过滤掉我发现的所有部分 [英] How to filter out all the sesions I found using get function
问题描述
这是我正在获取的会话列表,现在我想删除所有会话,除非活动的是true
This is the list of session I am getting, Now I want delete all the sessions except where active is true
const token = 'xxxx-xxx-xxxxx-xxxx-xxxxx';
const session_list = [
{
"id": "45345345-4534-5435-d1cc1bdb6153410",
"device": "browser",
"ip": "xx.xx.xxx.xxx",
"city": null,
"country": null,
"browser_name": "Opera",
"browser_version": "67.0.3575.115",
"os_name": "Mac OS"
},
{
"id": "23213-34234-324234-4234324o",
"device": "browser",
"ip": "xx.xx.xxx.xxx",
"city": null,
"country": null,
"browser_name": "Mozila",
"browser_version": "67.0.3575.115",
"os_name": "Windows"
},
{
"id": "324234-sadasd34-sdsda343-3434234234",
"device": "browser",
"ip": "xx.xx.xxx.xxx",
"city": null,
"country": null,
"browser_name": "Opera",
"browser_version": "67.0.3575.115",
"os_name": "android",
"active": true
}
]
现在,我想调用一个API来删除会话,现在它包含3个内容:静态令牌和ip
,session_id
,我可以在session_list
数组中找到它们
现在,API通过传递必需的参数来一一删除会话.
Now I want to call an API to delete the session, Now it takes 3 things in it, token which is static, and ip
, session_id
which i find in array of the session_list
Now the API delete session one by one by passing the required arguments.
const delete_session_api = async (data) => {
// delete the session
try {
const config = {
data: querystring.stringify(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
const { data: api_res } =
await axios.delete('/sessions/delete', config);
return api_res;
} catch (error) {
throw error;
}
};
所有我想从session_list
中删除会话,但活动的true会话是相同的列表.
有人可以帮助我实现这一目标的有效方法吗?
All I want to delete the sessions from the session_list
except the active true one which is the same list.
Can any one help me with the efficient way to do this?
推荐答案
您可以直接使用Array.filter()方法,如下所示.
You can use Array.filter() method directly as below.
说,如果您将其作为API响应的一部分,并且尝试在成功回调中处理它,并想返回活动会话:
Say, if you're getting this as part of an API response and you're trying to handle it in the success callback and want to return the active sessions:
const activeSessions = response.data.filter(session => session.active);
或
return response.data.filter(session => session.active);
注意:如果条件适用于多个会话,则结果可能包含多个对象.
请参阅Mozilla开发人员网络Webdocs,以更好地了解Array.filter(). 在此处链接
Refer Mozilla Developer Network Webdocs to understand Array.filter() better. Link here
更新
有了新的要求,您希望首先获得不活动的会话,并针对每个会话(一个接一个)进行API调用以删除它们.
With the new requirement, you want to first get the sessions which are inactive and make API calls for each (one after the other) to delete them.
- 获取不活动会话的列表.即
active
为not true
的会话
- Get the list of inactive sessions. i.e., sessions where the
active
isnot true
const inactiveSessionsList = sessions_list.filter(session => !session.active)
- 有了此列表,您可以按以下方式进行api调用.
inactiveSessionsList.forEach(async (session)=> {
const payload = {token: token, ip: session.ip, session_id: session.id }
await delete_session_api(payload);
})
根据inactiveSessionsList中的项目数量,您将触发每个非活动会话的API调用.
Based on the number of items in the inactiveSessionsList, you will be triggering API calls for each inactive session.
这篇关于如何使用get函数过滤掉我发现的所有部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!