daify
v1.0.0
Published
dictify and arrayify
Downloads
3
Readme
daify
dictify and arrayify
API
opts
Object
string
shortcut for opts.by
dictify(array, opts)
Make array
into dict
const opts = {
by: "id",
include: {
lists: "id",
"namespace.inners": "id"
}
};
const array = [
{ id: 1, lists: [{ id: 1 }] },
{ id: 2, lists: [{ id: 1 }] },
{
id: 3,
lists: [{ id: 1 }],
namespace: {
inners: [{ id: 1 }, { id: 2 }, { id: 3 }]
}
}
];
log(dictify(array, opts));
/*
{
"1": {
"id": 1,
"lists": {
"1": {
"id": 1
}
}
},
"2": {
"id": 2,
"lists": {
"1": {
"id": 1
}
}
},
"3": {
"id": 3,
"lists": {
"1": {
"id": 1
}
},
"namespace": {
"inners": {
"1": {
"id": 1
},
"2": {
"id": 2
},
"3": {
"id": 3
}
}
}
}
}
*/
arrayify(dict, opts)
Make dict
into array
const opts = {
by: "id", // by is useless here
include: {
lists: "id",
"namespace.inners": "id"
}
};
const dict = {
"1": {
id: 1,
lists: {
"1": { id: 1 }
}
},
"2": {
id: 2,
lists: {
"1": { id: 1 }
}
},
"3": {
id: 3,
lists: { "1": { id: 1 } },
namespace: {
inners: {
"1": { id: 1 },
"2": { id: 2 },
"3": { id: 3 }
}
}
}
};
log(arrayify(dict, opts));
/*
[
{
"id": 1,
"lists": [
{
"id": 1
}
]
},
{
"id": 2,
"lists": [
{
"id": 1
}
]
},
{
"id": 3,
"lists": [
{
"id": 1
}
],
"namespace": {
"inners": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
}
]
*/