npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-of-object-query

v1.0.1

Published

array-of-object-query is a simple JavaScript library that makes working with arrays of objects easier. It has some methods to help you filter, sort, group, and summarize data. Use this library to make handling complex data structures simple and efficient.

Downloads

10

Readme

array-of-object-query

array-of-object-query is a simple JavaScript library that makes working with arrays of objects easier. It has some methods to help you filter, sort, group, and summarize data. Use this library to make handling complex data structures simple and efficient.

Installation

To install this package, simply run the following command with npm:

$ npm install array-of-object-query

Usage

To use the custom array methods provided by the array-of-object-query package, just import the methods:

import arrayQuires from "array-of-object-query";

Apply the Methods

After import the method, you can apply the methods directly to your arrays of objects. Here's an example using the where method:

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 34, occupation: "Designer" },
  { name: "Charlie", age: 25, occupation: "Teacher" },
];

// Example usage of where method
const engineers = people.where("occupation", "Engineer");
console.log(engineers);
// Output: [{ name: 'Alice', age: 28, occupation: 'Engineer' }]

Available Methods

All of the available methods are provided below

where(key, value)

Description

Filters the array based on key-value pairs.

Parameters

  • key: The key to filter objects by.
  • value: The value to filter objects against.

Returns

  • An array of objects that match the specified key-value pair.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const engineers = people.where("occupation", "Engineer");
// Output: [{ name: "Alice", age: 28, occupation: "Engineer" }]

const teacher = people.where("occupation", "Teacher").where("age", 30);
// Output: [{ name: "Charlie", age: 30, occupation: "Teacher" }]

whereIn(key, values)

Description

Filters the array where a key's value is in an array of values.

Parameters

  • key: The key to filter by.
  • values: An array of values to match against the key.

Returns

  • An array containing objects where the specified key's value matches any of the provided values.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const filtered = people.whereIn("age", [25, 28]);
// Output: [{ name: "Alice", age: 28, occupation: "Engineer" },{ name: "Bob", age: 25, occupation: "Designer" }]

select(key)

Description

Creates a new array with objects containing only the specified key.

Parameters

  • ...keys: One or more keys to select from each object.

Returns

  • An array of objects with only the specified key and its corresponding values.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const names = people.select("name");
// Output: [{ name: "Alice" }, { name: "Bob" }, { name: "Charlie" }]

const namesAndAges = people.select("name", "age");
// Output: [{ name: "Alice", age: 28 }, { name: "Bob", age: 25 }, { name: "Charlie", age: 30 }]

pluck(...keys)

Description

Returns arrays of values for specified keys from each object in the array.

Parameters

  • ...keys: One or more keys to pluck values from each object.

Returns

  • An array of arrays containing values corresponding to the specified keys from each object.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const pluckedValues = people.pluck("name", "age");
// Output: [["Alice", "Bob", "Charlie"], [28, 25, 30]]

orderBy(key, order='asc')

Description

Sorts the array of objects based on a specified key and order.

Parameters

  • key: The key to sort by.
  • order (optional): Sorting order, either 'asc' (default) or 'desc'.

Returns

  • The sorted array of objects.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const sortedByNameAsc = people.orderBy("name");
// Output: [{ name: "Alice", age: 28, occupation: "Engineer" }, { name: "Bob", age: 25, occupation: "Designer" }, { name: "Charlie", age: 30, occupation: "Teacher" }]

const sortedByAgeDesc = people.orderBy("age", "desc");
// Output: [{ name: "Charlie", age: 30, occupation: "Teacher" }, { name: "Alice", age: 28, occupation: "Engineer" }, { name: "Bob", age: 25, occupation: "Designer" }]

first()

Description

Retrieves the first element of the array.

Parameters

  • None

Returns

  • The first element of the array, or undefined if the array is empty.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const firstPerson = people.first();
// Output: { name: "Alice", age: 28, occupation: "Engineer" }

last()

Description

Retrieves the last element of the array.

Parameters

  • None

Returns

  • The last element of the array, or undefined if the array is empty.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const lastPerson = people.last();
// Output: { name: "Charlie", age: 30, occupation: "Teacher" }

min(key)

Description

Finds the minimum value of a specified numeric property across all objects in the array.

Parameters

  • key: The key representing the numeric property to find the minimum value for.

Returns

  • The minimum value of the specified numeric property, or undefined if the array is empty or if the property values are not numeric.

Example

const people = [
  { name: "Alice", age: 28 },
  { name: "Bob", age: 25 },
  { name: "Charlie", age: 30 },
];

const minAge = people.min("age");
// Output: 25

max(key)

Description

Finds the maximum value of a specified numeric property across all objects in the array.

Parameters

  • key: The key representing the numeric property to find the maximum value for.

Returns

  • The maximum value of the specified numeric property, or undefined if the array is empty or if the property values are not numeric.

Example

const people = [
  { name: "Alice", age: 28 },
  { name: "Bob", age: 25 },
  { name: "Charlie", age: 30 },
];

const maxAge = people.max("age");
// Output: 30

take(count)

Description

Retrieves a specified number of elements from the beginning of the array.

Parameters

  • count: The number of elements to retrieve from the beginning of the array.

Returns

  • An array containing the specified number of elements from the beginning of the original array.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
];

const firstTwoPeople = people.take(2);
// Output: [{ name: "Alice", age: 28, occupation: "Engineer" }, { name: "Bob", age: 25, occupation: "Designer" }]

sum(key)

Description

Calculates the sum of a specified numeric property across all objects in the array.

Parameters

  • key: The key representing the numeric property to calculate the sum for.

Returns

  • The sum of the specified numeric property, or 0 if the array is empty or if the property values are not numeric.

Example

const expenses = [
  { item: "Rent", amount: 1000 },
  { item: "Groceries", amount: 300 },
  { item: "Utilities", amount: 200 },
];

const totalExpenses = expenses.sum("amount");
// Output: 1500

average(key)

Description

Calculates the average value of a specified numeric property across all objects in the array.

Parameters

  • key: The key representing the numeric property to calculate the average for.

Returns

  • The average value of the specified numeric property, or undefined if the array is empty or if the property values are not numeric.

Example

const scores = [
  { student: "Alice", score: 85 },
  { student: "Bob", score: 90 },
  { student: "Charlie", score: 75 },
];

const averageScore = scores.average("score");
// Output: 83.33

groupBy(key)

Description

Groups elements of the array based on a specified key.

Parameters

  • key: The key by which to group the elements.

Returns

  • An object where keys are unique values of the specified key, and values are arrays of objects that share the same key value.

Example

const people = [
  { name: "Alice", age: 28, occupation: "Engineer" },
  { name: "Bob", age: 25, occupation: "Designer" },
  { name: "Charlie", age: 30, occupation: "Teacher" },
  { name: "David", age: 28, occupation: "Developer" },
];

const groupedByAge = people.groupBy("age");
// Output:
// {
//   '25': [{ name: "Bob", age: 25, occupation: "Designer" }],
//   '28': [
//     { name: "Alice", age: 28, occupation: "Engineer" },
//     { name: "David", age: 28, occupation: "Developer" }
//   ],
//   '30': [{ name: "Charlie", age: 30, occupation: "Teacher" }]
// }

unique(key)

Description

Returns an array of unique objects based on a specified key.

Parameters

  • key: The key representing the property to determine uniqueness.

Returns

  • An array containing unique objects based on the specified key.

Example

const items = [
  { id: 1, name: "Apple" },
  { id: 2, name: "Orange" },
  { id: 1, name: "Apple" },
  { id: 3, name: "Banana" },
];

const uniqueItems = items.unique("id");
// Output: [{ id: 1, name: "Apple" }, { id: 2, name: "Orange" }, { id: 3, name: "Banana" }]

distinct(key)

Description

Returns an array of distinct values of a specified property across all objects in the array.

Parameters

  • key: The key representing the property to retrieve distinct values for.

Returns

  • An array containing distinct values of the specified property.

Example

const items = [
  { id: 1, name: "Apple" },
  { id: 2, name: "Orange" },
  { id: 1, name: "Apple" },
  { id: 3, name: "Banana" },
];

const distinctIds = items.distinct("id");
// Output: [1, 2, 3]

License

array-of-object-query is created by Fahim Muntasir . Released under the MIT license.