Question: I am looking for a MYSQL paging solution with last\_id and order condition, to use LIMIT, OFFSET I need to figure out the offset for the last\_id.

For example, I have a table news (id, title, updated\_at), the first page returns 3,1,2,5,4, for the second page I have 3 arguments (last\_id is 4, order by updated\_at desc, limit is 5), I need to figure out the offset for the id 4 order by updated\_at desc.

```
+------+-------+---------------------+
| id   | title | updated_at          |
+------+-------+---------------------+
|    3 | test3 | 2018-01-22 11:30:08 |
+------+-------+---------------------+
|    1 | test1 | 2018-01-21 12:30:08 |
+------+-------+---------------------+
|    2 | test2 | 2018-01-20 12:30:08 |
+------+-------+---------------------+
|    5 | test5 | 2018-01-19 13:30:08 |
+------+-------+---------------------+
|    4 | test4 | 2018-01-18 14:30:08 |
+------+-------+---------------------+

....

+------+-------+---------------------+
|    11 | test11 | 2018-01-17 14:30:08 |
+------+-------+---------------------+
|    12 | test12 | 2018-01-16 15:30:08 |
+------+-------+---------------------+

```

What I want to return for the second page is 11,12,...., is there any efficient way to achieve it?

Answer: 
you can do this with only one `reduce()`:

```
const mapStateToProps = (state, ownProps) => {
    console.log("mapStateToProps", state.installations);
    let {assignedInstallations, 
         unassignedInstallations } = Object.keys(state.installations)
          .reduce(function(acc, cur, i){
             if(state.installations[i].operator == state.login){
               acc.assignedInstallations.push(state.installations[i]);
             }else{
               acc.unassignedInstallations .push(state.installations[i]);
             }
             return acc
           }, {assignedInstallations: [], unassignedInstallations: [] })

    return {
        assignedInstallations,
        unassignedInstallations,
        loginUserId: state.login
    };
};

```
`lodash` (An utility library) have a notion of collection (Here is an example <https://lodash.com/docs/4.17.4#filter> for `filter` function). It takes as input Object or Array and returns an Array. It seems to fit to your needs. Here is the refactored code: 

```
import {
  filter,
} from 'lodash'

const mapStateToProps = (state, ownProps) => {
    let assignedInstallations = filter(state.installations, installation => installation.operator == state.login);
    let unassignedInstallations = filter(state.installations, installation => installation.operator != state.login);
    return {
        assignedInstallations,
        unassignedInstallations,
        loginUserId: state.login
    };
};

```