0

Giving a code as below. What is the meaning of [] in return statement? Is it possible to remove [] like sortField: item.sortField

[sortField]: item[sortField]

is it an array or object?

function generatePaginationQuery(query, sort, nextKey) {
  const sortField = sort == null ? null : sort[0];

  function nextKeyFn(items) {
    if (items.length === 0) {
      return null;
    }

    //Get the last item
    const item = items[items.length - 1];

    if (sortField == null) {
      return { _id: item._id };
    }

    return { _id: item._id, [sortField]: item[sortField] };
  }

  if (nextKey == null) {
    return { paginatedQuery: query, nextKeyFn };
  }

  let paginatedQuery = query;

  if (sort == null) {
    paginatedQuery._id = { $gt: nextKey._id };
    return { paginatedQuery, nextKey };
  }

}
truongt2
  • 7
  • 4
  • 2
    Read about [computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names) – Gabriele Petrioli Jun 06 '23 at 15:20
  • Here an [article](https://linuxhint.com/dynamic-object-key-in-javascript/) about this dinamic property feature in js – Wuagliono Jun 06 '23 at 15:25

0 Answers0