-1

I have an object that has fields like the following image:

object

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.

online.0227
  • 640
  • 4
  • 15
  • 29
  • You have an object, not an array so it should be `jitsi['155a37bf']._displayName` – Phil Aug 02 '22 at 01:19
  • @Phil This doesn't work. I get an error like "Logger.js:154 2022-08-02T01:32:57.973Z [modules/xmpp/strophe.util.js] : Strophe: TypeError: Cannot read properties of undefined (reading '_displayName')". Thank you very much. very strange output though, as I only did console.log(jitsi['155a37bf']._displayName); – online.0227 Aug 02 '22 at 01:33
  • If I had to guess (and I do), you aren't waiting for this object to populate with data. See that little blue _i_ next to the object in your screenshot, it explains things – Phil Aug 02 '22 at 01:44
  • @Phil I guess that's right, it says "this value was evaluated upon first expanding. It may have changed since then". Maybe I need to capture this value before being changed? by making const or something Thank you. – online.0227 Aug 02 '22 at 02:00

1 Answers1

1

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; }
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Thank you very much, but this doesn't work. – online.0227 Aug 02 '22 at 01:27
  • @online.0227 how **exactly** does it _"not work"_? – Phil Aug 02 '22 at 01:43
  • it says "this value was evaluated upon first expanding. It may have changed since then" on blue icon you mentioned. I guess this answer only work if I can capture before the jitsi variable got changed to empty somehow. Currently, after what is suggested here, it just shows empty array. I tried Object.freeze or const right before console.log, but both doesn't work. – online.0227 Aug 02 '22 at 02:21
  • It's more likely that you're trying to operate on it when it's empty and it becomes full later on. The console is weird like that. If you want to see the state of a variable at the time you log it, I recommend `console.log("jitsi", JSON.stringify(jitsi, null, 2))` – Phil Aug 02 '22 at 02:23
  • @online.0227 see this answer for more details. [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/283366) – Phil Aug 02 '22 at 02:24
  • I think this is correct. After I run cosole.log after 2 second, it actually output final data. Thank you very much – online.0227 Aug 02 '22 at 03:11