Home>

There is an object with the embedded structure. Add an item to the object is simple, but it fails to be removed. Tell me how to remove the nested key correctly: value.

Code and object structure:

const _= Require ('Lodash')
.
.
.
Let OBJ_A= {}
Let OBJ_B= {}
Let Objid, ReceiveDKey, ReceivedValue= NULL
Socketio.USE (FUNCTION (Socket, Next) {
    Objid= Socket.Handshake.Query.Objid
    ReceivedKey= Socket.Handshake.Query.rkey
    ReceivedValue= Socket.Handshake.Query.RValue.
    If (Objid &
&
 ReceivedKey &
&
 ReceivedValue) {
        //Check for the presence of the desired object, add empty
        if (OBJ_A [OBJID]=== undefined) {OBJ_A [OBJID]= OBJ_B}
        //Add data to the object
        OBJ_A [OBJID] [ReceivedKey]= ReceivedValue
        RETURN NEXT ()
    }
    NEXT (New Error ('Authentication Error'))
}
socketio.sockets.on ('Connection', (Socket)= >
 {
.
.
.
    socket.on ('disconnect', ()= >
 {
        //I'm trying to remove in different ways ... unsuccessfully
        //-------------------------------------------------------------
        //_.Remove (OBJ_A [OBJID], (U)= >
 U=== ReceivedKey)
        //Delete OBJ_A [OBJID] [ReceivedKey]
        //_.pull (OBJ_A [OBJID], {ReceiveDKey: ReceivedValue})
        Console.log (JSON.Stringify (OBJ_A [OBJID]))
    })

And you can find out the structure of the object? What nesting?

Randall2021-06-18 10:21:24

Lododash.kom /Doks /4.17.15 # otitis

nörbörnën2021-06-18 10:25:44
  • Answer # 1

    It all depends on the object nesting and is the object mutation is allowed.

    if allowed can be usedUnset

    const OBJ= {
      A: {
        Name: "Some"
      }
      B: {
        G: {
          Find: True.
        }
      }
      ID: "88"
    }
    UNSET (OBJ, "B.G.FIND");
    Console.log (Obj.b.g.find); //undefined.
    

    If the mutation is not allowed, then you can useOmit

    const OBJ= {
      A: {
        Name: "Some"
      }
      B: {
        G: {
          Find: True.
        }
      }
      ID: "88"
    }
    const a= omit (OBJ, "B.G.FIND"); //omit Returns a new object
    Console.log (A.B.Find); //undefined.
    

    Removing the object's key you also delete its value.