oa2o
v1.1.6
Published
[oa2o](https://www.npmjs.com/package/oa2o) (object array to object) is a simple npm module to convert iterative object array ( [{k:"a"}, {k:"b"}] ) to object ( {a:{k:"a"}, b:{k:"b"}} ):
Downloads
28
Maintainers
Readme
About
oa2o (object array to object) is a simple npm module to convert iterative object array ( [{k:"a"}, {k:"b"}] ) to object ( {a:{k:"a"}, b:{k:"b"}} ):
let oa = [{k:"a"}, {k:"b"}];
let ob = oa2o(oa, "k");
console.log(ob); // ob = {a:{k:"a"}, b:{k:"b"}}
eg2
let oa = [{k:"a", k2:"a2"}, {k:"b", k2:"b2"}];
let ob = oa2o(oa, "k", "k2"); // ob = {a:"a2", b:"b2"}
eg3
let oa = [{k:"a", k2:"a2", k3:"a3"}, {k:"b", k2:"b2", k3:"b3"}];
let ob = oa2o(oa, "k", ["k2","k3"]); // ob = {a:{k2:"a2",k3:"a3"}, b:{k2:"b2", k3:"b3"}}
eg4 (rename object key)
let oa = [{k:"a", k2:"a2"}, {k:"b", k2:"b2"}];
let ob = oa2o(oa, "k", [["k2","n2"]]); // ob: {a:{n2:"a2"}, b:{n2:"b2"}}
eg5 (rename + get nested object key value)
let oa = [
{ k: "a", k2: { k3: 3 } },
{ k: "b", k2: { k3: 5 } },
];
let ob = oa2o(oa, "k", [["k2.k3", "n2"]]); // ob: { a: { n2: 3 }, b: { n2: 5 } }
let ob = oa2o(oa, "k", [[["k2","k3"], "n2"]]); // 2nd way
Installation
npm i oa2o
Syntax & Parameters
oa2o(ar, key[, valKey])
- ar = (array) object array ( [{k:"a"}, {k:"b"}] )
- key = (string) key in object array; the value of this specified key will be used as the key for new object; usually choose key with unique value.
- valKey = (string|array) key in object array; the value of this specified key will be used as the value for the key in new object, instead of the whole original object element.
return
return object
Advanced
Return object with shallow copy.