Json-Function provides a lot of useful functions especially for your json data. It contains the methods you need too much to eliminate unnecessary code repetition.
You can use the Json-Function methods separately, but it is possible to use them all together. You can also chain it.
Copy $ npm install json-function
Copy import JsonFunction from 'json-function';
Copy const data = [
{
userId: 1,
id: 1,
title: "delectus aut autem",
completed: false
},
{
userId: 1,
id: 2,
title: "quis ut nam facilis et officia qui",
completed: false
},
{
userId: 1,
id: 3,
title: "fugiat veniam minus",
completed: false
},
{
userId: 1,
id: 4,
title: "et porro tempora",
completed: true
}
];
Copy
JsonFunction
.where({ completed: false })
.select(["title", "completed"])
.orderBy("title", "DESC")
.limit(2)
.get(data);
Copy [
{
title: "quis ut nam facilis et officia qui",
completed: false
},
{
title: "fugiat veniam minus",
completed: false
}
];
Copy JsonFunction.where({ completed: false });
JsonFunction.select(["title", "completed"]);
JsonFunction.orderBy("title", "DESC");
JsonFunction.limit(2);
const result = JsonFunction.get(data);
Create a query and use it at any time.
Copy const queryTwoIncompleteTasks = JsonFunction
.where({ completed: false })
.select(["title", "completed"])
.limit(2)
.getQuery();
Copy JsonFunction.setQuery(queryTwoIncompleteTasks).get(data);
Copy JsonFunction.get(data, { query: queryTwoIncompleteTasks });