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

@dsbasko/merge-and-map

v1.0.0

Published

This function is used to merge two arrays of objects and map them to a new array of objects.

Downloads

1

Readme

Install

npm i @dsbasko/merge-and-map

or

yarn add @dsbasko/merge-and-map

Usage

The function mergeAndMap takes four arguments:

arr1: an array of objects that need to be merged with objects from arr2.

arr2: an array of objects to merge with objects from arr1.

fields: an array of two keys that indicate which fields in the objects from arr1 and arr2 should be used for merging.

tpl: a function that takes two arguments (an object from arr1 and an object from arr2) and returns a new object that will be added to the resulting array. If the tpl function returns null, the object will not be added to the resulting array.

The mergeAndMap function merges objects from the arr1 and arr2 arrays, using the values of the fields[0] and fields[1] fields as keys. If an object with the specified key is found in the arr2 array, the tpl function is called with two arguments (an object from arr1 and an object from arr2), and the result is added to the resulting array. If the object is not found or the tpl function returns undefined, the object is not added to the resulting array.

Example

import { mergeAndMap } from '@dsbasko/merge-and-map';

const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Alice' },
  { id: 3, name: 'Bob' },
];

const info = [
  { userId: 1, address: '123 Main St' },
  { userId: 2, address: '456 Elm St' },
];

const result = mergeAndMap(users, info, ['id', 'userId'], (user, info) => ({
  ...user,
  address: info.address,
}));

console.log(result);
/*
[
  { id: 1, name: 'John', address: '123 Main St' },
  { id: 2, name: 'Alice', address: '456 Elm St' },
]
*/

In the example, we merge the users and info arrays using the id and userId fields. The tpl function merges objects by adding the address field to objects from users if the corresponding object is found in info. The result of the mergeAndMap function is an array of objects containing merged objects from users and info.

Author

Dmitriy Basenko [email protected], GitHub, Twitter, Telegram