- Description
- This functions turns a JSON string into a searchable
Collection
.
- This functions turns a JSON string into a searchable
- Example
import { accio } from "@drashland/accio"; import { readFileSync } from "fs"; const data = readFileSync("./data.json", "utf-8"); const collection = accio(data); // The JSON data is now a searchable collection
This is a searchable object with methods to help you narrow down your search to a specific item within the collection.
A collection is a class that wraps itself around an array or object. When
constructed, the array or object is stored in the collection's data
property.
This data property is searched in the collection's methods.
- Description
- This method targets an array in the collection that is an object so that you can search it further.
- Returns
- A
Collection
object that can be searched further. If this is the array you want to get, then you can call.get()
on theCollection
and it will return the array.
- A
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: ["hello from field_1_1"], }, field_2: "hello world", field_3: ["hello"], }); const array = accio(data) .object("field_1") // Target the object with the name "field_1" .array("field_1_1"); // Target the array in the object with the name "field_1_1" console.log(array); // Outputs Collection {} console.log(array.get()); // Outputs [ "hello from field_1_1" ]
- Description
- This method finds objects in the collection that is an array that match the given fields so that you can search them further.
- Returns
- The
Collection
object that it searched so you can search it further using anyCollection
method. If the items it found are the items you want to get, then you can call.get()
on theCollection
and it will return the items in an array.
- The
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: ["hello from field_1_1"], }, field_2: "hello world", field_3: [ { name: "hello", slug: "world_1", }, { name: "world", slug: "world_1", }, { name: "world", slug: "world_2", }, ], }); const result = accio(data) .array("field_3") // Target the array with the name "field_3" .find({ // In that array, find all objects with a "name" field ... name: "world", // ... that has a value of "world" }); console.log(result); // Outputs Collection {} console.log(result.get()); // Outputs [ { name: "world", slug: "world_1" }, { name: "world", slug: "world_2" } ]
- Description
- This method finds objects in the collection that is an array that match the given fields so that you can search them further.
- Returns
- The first item that matched the fields as a
Collection
that is an object. If this is the object you want to get, then you can call.get()
on theCollection
and it will return the object.
- The first item that matched the fields as a
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: ["hello from field_1_1"], }, field_2: "hello world", field_3: [ { name: "hello", slug: "world_1", }, { name: "world", slug: "world_1", }, { name: "world", slug: "world_2", }, ], }); const result = accio(data) .array("field_3") // Target the array with the name "field_3" .findOne({ // In that array, find the first matching object with a "name" field ... name: "world", // ... that has a value of "world" }); console.log(result); // Outputs Collection {} console.log(result.get()); // Outputs { name: "world", slug: "world_1" }
- Description
- This method returns the first item in the
Collection
that is an array so that you can search it further.
- This method returns the first item in the
- Returns
- The first item in the
Collection
that is an array. If this is the object you want to get, then you can call.get()
on theCollection
and it will return the object.
- The first item in the
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: ["hello from field_1_1"], }, field_2: "hello world", field_3: [ { name: "hello", slug: "world_1", }, { name: "world", slug: "world_1", }, { name: "world", slug: "world_2", }, ], }); const result = accio(data) .array("field_3") // Target the array with the name "field_3" .first(); // Get the first item in the array console.log(result); // Outputs Collection {} console.log(result.get()); // Outputs { name: "world", slug: "world_1" }
- Description
- This method gets the collection in its raw form.
- Returns
- The collection in its raw form. If the collection is an array, then it returns an array. If the collection is an object, then it returns an object.
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { "hello": "world" }, field_2: "hello world", field_3: ["hello"], }); const array = accio(data) .array("field_3"); console.log(array.get()); // Outputs [ "hello" ] const object = doc .object("field_1"); console.log(object.get()); // Outputs { hello: "world" }
- Description
- This method targets an object in the collection that is an object so that you can search it further.
- Returns
- A
Collection
object that can be searched further. If this is the object you want to get, then you can call.get()
on theCollection
and it will return the object.
- A
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: { field_1_1_1: ["hello from field_1_1_1"], }, }, field_2: "hello world", field_3: [ { name: "hello", slug: "world_1", }, { name: "world", slug: "world_1", }, { name: "world", slug: "world_2", }, ], }); const result = accio(data) .object("field_1") // Target the object with the name "field_1" .object("field_1_1"); // In that object, target the object with the name "field_1_1" console.log(result); // Outputs Collection {} console.log(result.get()); // Outputs { field_1_1_1: [ "hello from field_1_1_1" ] }
See the Searching tutorial for more information on this API.
- Description
- This method calls
JSON.stringify()
on the collection.
- This method calls
- Returns
- A JSON string.
- Example
import { accio } from "@drashland/accio"; const data = JSON.stringify({ field_1: { field_1_1: { field_1_1_1: ["hello from field_1_1_1"], }, }, field_2: "hello world", field_3: [ { name: "hello", slug: "world_1", }, { name: "world", slug: "world_1", }, { name: "world", slug: "world_2", }, ], }); const result = accio(data) .object("field_1") // Target the object with the name "field_1" .object("field_1_1") // In that object, target the object with the name "field_1_1" .stringify(); // Call JSON.stringify() on "field_1_1" console.log(result); // Outputs {"field_1_1_1":["hello from field_1_1_1"]}