Skip to content

listify

Convert an object to a list

145 bytes

Usage

Given an object and a mapping function, return an array with an item for each entry in the object.

import * as
import _
_
from 'radashi'
const
const fish: {
marlin: {
weight: number;
};
bass: {
weight: number;
};
}
fish
= {
marlin: {
weight: number;
}
marlin
: {
weight: number
weight
: 105,
},
bass: {
weight: number;
}
bass
: {
weight: number
weight
: 8,
},
}
import _
_
.
listify<{
weight: number;
} | {
weight: number;
}, "marlin" | "bass", {
name: "marlin" | "bass";
weight: number;
} | {
name: "marlin" | "bass";
weight: number;
}>(obj: Record<"marlin" | "bass", {
weight: number;
} | {
...;
}>, toItem: (key: "marlin" | "bass", value: {
weight: number;
} | {
...;
}) => {
...;
} | {
...;
}): ({
...;
} | {
...;
})[]
export listify

Convert an object to a list, mapping each entry into a list item.

@seehttps://radashi.js.org/reference/object/listify

@example

const a = { a: 1, b: 2, c: 3 }
listify(a, (key, value) => ({ key, value }))
// => [
// { key: 'a', value: 1 },
// { key: 'b', value: 2 },
// { key: 'c', value: 3 }
// ]

@version12.1.0

listify
(
const fish: {
marlin: {
weight: number;
};
bass: {
weight: number;
};
}
fish
, (
key: "marlin" | "bass"
key
,
value: {
weight: number;
} | {
weight: number;
}
value
) => ({ ...
value: {
weight: number;
} | {
weight: number;
}
value
,
name: "marlin" | "bass"
name
:
key: "marlin" | "bass"
key
})) // => [{ name: 'marlin', weight: 105 }, { name: 'bass', weight: 8 }]