How would I convert a object to an array of objects while keeping key names?
// actual
obj = {
key1: null,
key2: "Nelly",
key3: [ "suit", "sweat" ]
}
// expected
arr = [
{ key2: "Nelly" },
{ key3: [ "suit", "sweat" ] }
]
currently my solution is...
var arr = Object.keys(obj).map(key => { if (obj[key]) return { key: obj[key] } });
which returns
arr = [
undefined,
{ key: "Nelly" },
{ key: [ "suit", "sweat" ] }
]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…