[]
        
(Showing Draft Content)

Search Table Data

The search method retrieves records that exactly match a specified value in a given field.

Search characteristics:

  • Case-sensitive.

  • Exact match only.

  • Returns all matching records.

  • Returns an empty array if no match is found.

  • Returns an empty array if the field does not exist.

  • Uses an index if available.

  • Falls back to linear scanning if no index exists.

Using search()

table.search(value, field);

Parameters

  • value - The value to search. Non-string values are internally converted to string.

  • field - The target field name.

Return Value

Returns an array of matching record objects.

Example result:

[
  { Month: "Jan", Sales: 120 }
]

If no record matches:

[]

Example with Index

var dataManager = spread.dataManager();

var supplierTable = dataManager.addTable("Supplier", {
  remote: {
    read: { url: apiUrl }
  }
});

supplierTable.fetch().then(function () {
  supplierTable.createIndexes(["CompanyName", "City"]);

  var result =
    supplierTable.search("New Carlisle", "City");

  console.log(result);
});

Assumed Data

Assume the table contains the following records:

[
  { SupplierID: 1, CompanyName: "Tokyo Traders", City: "Tokyo" },
  { SupplierID: 2, CompanyName: "Grand Foods", City: "New Carlisle" },
  { SupplierID: 3, CompanyName: "Sunrise Market", City: "New Carlisle" }
]

Search Result

[
  { SupplierID: 2, CompanyName: "Grand Foods", City: "New Carlisle" },
  { SupplierID: 3, CompanyName: "Sunrise Market", City: "New Carlisle" }
]

If "City" is indexed, the lookup uses the index.

If "City" is not indexed, the table performs a linear scan and returns the same result, but with lower performance on large datasets.