I have an object that has fields like the following image:
I want to have it like a plain json object. For example, if the variable name of this was "jitsi", then I should be able to check "displayName" of 155a37bf by coding like "jitsi[0].displayName". It also means that it should output "fwefe" if I code like "console.log(jitsi[0].displayName);".
However, there are so many underbars like _ in every keys.
It also needs to output length. For example, it should output 2 if I code like "console.log(jitsi.length);" as there are 2 entries named "155a37bf" and "908f57df" respectively.
How do I make it like a plain json object?
I already did things like following but it didn't work
Object.keys(jitsi).map((item) => {
arr.push({
category: item,
...jsonObj[item]
})
});
Thank you very much.
CodePudding user response:
You can convert this object to an array simply by using Object.values().
You can also remove the underscore prefixes using a map operation
// Example data
const jitsi = {
"155a37bf": {
_id: "155a37bf",
_displayName: "fewfe",
},
"908f57df": {
_id: "908f57df",
_displayName: "sender",
},
};
// Transform
const jitsiArray = Object.values(jitsi).map((obj) =>
Object.fromEntries(
Object.entries(obj).map(([key, val]) => [key.replace(/^_/, ""), val])
)
);
console.log(jitsiArray);
.as-console-wrapper { max-height: 100% !important; }