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

object-array-filter

v1.0.4

Published

Filters an array of objects by matching a field's value in each object to a search term. Returns filtered array

Downloads

2

Readme

object-array-filter

Explanation

Filters an array of objects by compariing a field's value in each object to a search term. The following operators can be used: "eq" | "ne" | "lt" | "gt" | "lte" | "gte". Returns a promise of a filtered array. Field values must be strings, numbers or booleans.

Installation & Usage

Run npm install --save object-array-filter

const find = require('object-array-filter');

/**
 * @param search object
 * @param array an array of object with the same properties
 * @param key <optional> object key to perform comparison.
 * @param comparison <optional> comparison operator to use.
 * @returns {Promise<object[] | []>} Returns filtered array
 */



const testArray = [
  { color: "black", size: 2, big: false },
  { color: "pink", size: 3, big: false },
  { color: "black", size: 3, big: false },
  { color: "pink", size: 14, big: true}
];

const size = 3;
const color = 'black';
const big = true;

const testing = async () => {
  try {
    const filteredSize = await find.default({ size }, testArray);
    const filteredColor = await find.default({ color }, testArray);
    const filteredNotBig = await find.default({ big }, testArray);
    console.log({ filteredSize, filteredColor, filteredBig });
  } catch (error) {
    console.log(error);
  }
};

//Run function
testing();

/* expected output: {
  filteredSize: [
    { color: 'pink', size: 3, big: false },
    { color: 'black', size: 3, big: false }
  ],
  filteredColor: [
    { color: 'black', size: 2, big: false },
    { color: 'black', size: 3, big: false }
  ],
  filteredBig: [ { color: 'pink', size: 14, big: true } ]
} */

/* 
NB: Variable name should match the name of the field in the object. 
If the variable name does not match the field name, the following syntax must be used
 */
const color2 = 'pink'
find.default({ color2 }, testArray, "color")
  .then((filtered) => console.log(filtered))
  .catch((error) => console.log(error));

/* expected output: [ { color: 'pink', size: 3 }, { color: 'pink', size: 14 } ] */

//NB: The default operator is 'eq'. If you wish to use another operator, below are some examples.
const testingOperators = async () => {
  try {
    const filteredLessThanOrEqual3 = await find.default({ size }, testArray, "size", "lte");
    const filteredGreaterThan3 = await find.default({ size }, testArray, "size", "gt");
    const filteredNotBlack = await find.default({ color }, testArray, "color", "ne");
    const filteredNotBig = await find.default({ big }, testArray, "big", "ne");
    console.log({ filteredLessThanOrEqual3, filteredGreaterThan3, filteredNotBlack, filteredNotBig });
  } catch (error) {
    console.log(error);
  }
};

testingOperators();

/* expected output: {
  filteredLessThanOrEqual3: [
    { color: 'black', size: 2, big: false },
    { color: 'pink', size: 3, big: false },
    { color: 'black', size: 3, big: false }
  ],
  filteredGreaterThan3: [ { color: 'pink', size: 14, big: true } ],
  filteredNotBlack: [
    { color: 'pink', size: 3, big: false },
    { color: 'pink', size: 14, big: true }
  ],
  filteredNotBig: [
    { color: 'black', size: 2, big: false },
    { color: 'pink', size: 3, big: false },
    { color: 'black', size: 3, big: false }
  ]
} */

TypeScript Usage

import find from 'object-array-filter';

/**
 * @param search object
 * @param array an array of object with the same properties
 * @param key <optional> object key to perform comparison.
 * @param comparison <optional> comparison operator to use.
 * @returns {Promise<object[] | []>} Returns filtered array
 */



const testArray = [
  { color: "black", size: 2, big: false },
  { color: "pink", size: 3, big: false },
  { color: "black", size: 3, big: false },
  { color: "pink", size: 14, big: true}
];

const size = 3;
const color = 'black';
const big = true;

const testing = async () => {
  try {
    const filteredSize = await find({ size }, testArray);
    const filteredColor = await find({ color }, testArray);
    const filteredNotBig = await find({ big }, testArray);
    console.log({ filteredSize, filteredColor, filteredBig });
  } catch (error) {
    console.log(error);
  }
};

//Run function
testing();

/* expected output: {
  filteredSize: [
    { color: 'pink', size: 3, big: false },
    { color: 'black', size: 3, big: false }
  ],
  filteredColor: [
    { color: 'black', size: 2, big: false },
    { color: 'black', size: 3, big: false }
  ],
  filteredBig: [ { color: 'pink', size: 14, big: true } ]
} */