How to filter out all the sesions I found using get function

自古美人都是妖i 提交于 2020-06-01 07:41:32

问题


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
}
]

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;

    }

    };

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?


回答1:


You can do this in vanilla JavaScript with this short function

const filterActiveSessions = payload => ({
  meta: payload.meta,
  data: (payload.data || []).filter(session => session.active),
})

You can also write a single function to delete non-active sessions using my functional programming library, rubico. I'm able to get it into a single function because I compose many small functions with the library.

const { pipe, fork, filter, get } = rubico

const payload = {
  "meta": {
    "message": "Sessions retrieved successfully."
  },
  "data": [
    {
      "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
    }
  ],
}

// payload => payload_with_active_sessions_only
const filterActiveSessions = fork({
  meta: get('meta'),
  data: pipe([
    get('data'),
    filter(session => session.active),
  ]),
})

console.log(
  filterActiveSessions(payload)
)
<script src="https://unpkg.com/rubico/index.js" crossorigin></script>



回答2:


You can use Array.filter() method directly as below.

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);

or

return response.data.filter(session => session.active);

Note: The result may contain more than one object if the condition is true for multiple sessions.

Refer Mozilla Developer Network Webdocs to understand Array.filter() better. Link here



来源:https://stackoverflow.com/questions/62106397/how-to-filter-out-all-the-sesions-i-found-using-get-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!