pickArray

A helpful utility to pick data from a store of list of objects by keys. If only one key is specified the derivative will be the array of values of that key else the derivative will an array of partial records of the keys mapped to store's list of values.

Demo

Store data:
[
  {
    "name": "Svelte",
    "author": "Rich",
    "createdAt": 1695166985461
  },
  {
    "name": "Vue",
    "author": "Evan",
    "createdAt": 1695166985461
  }
]
				
Picked Data 'name':
[
  "Svelte",
  "Vue"
]
				
Picked Data 'name' and 'author':
[
  {
    "name": "Svelte",
    "author": "Rich"
  },
  {
    "name": "Vue",
    "author": "Evan"
  }
]
				

Usage

<script lang="ts">
import { pick } from "svelte-legos";
import { writable } from "svelte/store";

const data = writable([
	{
		name: "Svelte",
		author: "Rich",
		createdAt: Date.now(),
	},
	{
		name: "Vue",
		author: "Evan",
		createdAt: Date.now(),
	},
]);

const names = pickArray(data, "name"); // $names => ["Svelte", "Vue"]

const namesAndAuthors = pickArray(data, "name", "author");
// $namesAndAuthors => [{ name: "Svelte", author: "Rich" }, { name: "Vue", author: "Evan" }]
</script>