The toKeyedArray function in the ts-array library converts a standard JavaScript object (record) into a proxy object that allows array-like operations. This function is particularly useful when you need to perform operations traditionally reserved for arrays (like map, reduce, forEach, filter) on object properties.
The toKeyedArray function takes an object as input and returns a proxy object. This proxy object mimics the behavior of an array, providing additional methods for object manipulation. It's designed to work with objects where the values are of a consistent type.
obj: The object to be converted into a proxy with array-like methods. It should be a record where keys are strings and values are of any consistent type. Returns A proxy object that enables array-like operations on the provided object. This includes methods like map, reduce, forEach, filter, slice, find, findKey, includes, keyOf, and lastKeyOf.
import { toKeyedArray } from 'ts-array'
const obj = { a: 1, b: 2 }
const keyedArray = toKeyedArray(obj)
const result = keyedArray.map((value, key) => value * 2)
console.log(result)
// Output:
// [2, 4]
import { toKeyedArray } from 'ts-array'
const obj = { a: 1, b: 2, c: 3 }
const keyedArray = toKeyedArray(obj)
const filtered = keyedArray.filter(value => value > 1)
console.log(filtered)
// Output:
// { b: 2, c: 3 }
import { toKeyedArray } from 'ts-array'
const obj = { a: 1, b: 2, c: 3 }
const keyedArray = toKeyedArray(obj)
const sum = keyedArray.reduce((acc, value) => acc + value, 0)
console.log(sum)
// Output:
// 6
import { toKeyedArray } from 'ts-array'
const obj = { a: 'first', b: 'second', c: 'third' }
const keyedArray = toKeyedArray(obj)
keyedArray.forEach((value, key) => console.log(key, value))
// Output:
// a first
// b second
// c third
import { toKeyedArray } from 'ts-array'
const obj = { a: 1, b: 2, c: 3, d: 4 }
const keyedArray = toKeyedArray(obj)
const sliced = keyedArray.slice(1, 3)
console.log(sliced)
// Output:
// [2, 3]
import { toKeyedArray } from 'ts-array'
const obj = { a: 10, b: 20, c: 30 }
const keyedArray = toKeyedArray(obj)
const found = keyedArray.find(value => value === 20)
console.log(found)
// Output:
// 20
import { toKeyedArray } from 'ts-array'
const obj = { a: 'apple', b: 'banana', c: 'cherry' }
const keyedArray = toKeyedArray(obj)
const foundKey = keyedArray.findKey(value => value === 'banana')
console.log(foundKey)
// Output:
// b
import { toKeyedArray } from 'ts-array'
const obj = { a: 'red', b: 'green', c: 'blue' }
const keyedArray = toKeyedArray(obj)
const hasGreen = keyedArray.includes('green')
console.log(hasGreen)
// Output:
// true
import { toKeyedArray } from 'ts-array'
const obj = { a: 'cat', b: 'dog', c: 'bird' }
const keyedArray = toKeyedArray(obj)
const keyOfDog = keyedArray.keyOf('dog')
console.log(keyOfDog)
// Output:
// b
import { toKeyedArray } from 'ts-array'
const obj = { a: 'circle', b: 'square', c: 'circle' }
const keyedArray = toKeyedArray(obj)
const lastKeyOfCircle = keyedArray.lastKeyOf('circle')
console.log(lastKeyOfCircle)
// Output:
// c