Delete an object from a nested array of objects

bob.mazzo

I have this small function (within my Angular 7 application) which uses JavaScript reduce(), and locates an object within a nested array of objects. I can then proceed to update certain properties on the fly.

Now, in addition to this find logic, I would like to also insert/delete an object into/from the nested array.

Question is: once I do locate my object, can I push() and/or delete an object ?

const input={UID:2,GUID:"",LocationName:"USA",ParentLocation:null,subs:[{UID:42,GUID:"",LocationName:"New Jersey",Description:"",subs:[{UID:3,GUID:"",LocationName:"Essex County",ParentLocation:null,"subs":[{UID:4,LocationName:"Newark",ParentLocation:3,"subs":[{"UID":49,"GUID":"","LocationName":"Doctor Smith's Office","LocationType":{"UID":2,"LocationTypeName":"Practice","Description":"other location"},"subs":[{"HostID":38,"HostName":"Ocean Host",}]}]}]}]}]};

const findUIDObj = (uid, parent) => {
  const { UID, subs } = parent;
  if (UID === uid) {
    const { subs, ...rest } = parent;
    return rest;
  }
  if (subs) return subs.reduce((found, child) => found || findUIDObj(uid, child), null);
};
console.log(findUIDObj(49, input));

var obj = findUIDObj(49, input);
delete obj; 

For example, in my Angular 7 app, it complains if I attempt to delete the found object:

ex/

var obj = findUIDObj(49, input);
delete obj; 

  'delete' cannot be called on an identifier in strict mode.
trincot

delete obj would never do what you want: first of all, it is not even an object from your input, since the function created a new object from the found object, excluding the subs property, and returned that. But more importantly, delete is used for deleting properties, not objects.

It seems you want to remove a matching object from its parent subs property. For that you would need to mutate the subs array, so it would exclude the matching object. For that to work in a generic way, your input should be an array. Otherwise that root object could not be removed from anything.

With that in mind, your lookup function should return the array in which the match was found and at which index. With those pieces of information you can decide to remove that element from the array, or to insert another object at that index.

Here is how it could work with removal:

const input=[{UID:2,GUID:"",LocationName:"USA",ParentLocation:null,subs:[{UID:42,GUID:"",LocationName:"New Jersey",Description:"",subs:[{UID:3,GUID:"",LocationName:"Essex County",ParentLocation:null,"subs":[{UID:4,LocationName:"Newark",ParentLocation:3,"subs":[{"UID":49,"GUID":"","LocationName":"Doctor Smith's Office","LocationType":{"UID":2,"LocationTypeName":"Practice","Description":"other location"},"subs":[{"HostID":38,"HostName":"Ocean Host",}]}]}]}]}]}];

const findUIDObj = (uid, arr) => {
    if (!arr) return;
    const idx = arr.findIndex(obj => obj.UID === uid);
    if (idx > -1) return [arr, idx];
    for (const obj of arr) {
        const result = findUIDObj(uid, obj.subs);
        if (result) return result;
    }
};
console.log(findUIDObj(49, input));

const [arr, idx] = findUIDObj(49, input) || [];
if (arr) {
    arr.splice(idx, 1); // Remove object from its parent array
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

How to generate a nested array of objects from an source json object

From Dev

Delete last object from the array of objects.

From Dev

removing object from nested array of objects mongodb

From Dev

Delete an object from nested array in openjson SQL Server 2016

From Dev

Create nested object from array of objects in JavaScript

From Dev

Create new object from array of nested arrays that contains objects

From Dev

How delete an object based on its index from a nested array in React?

From Dev

Remove a specific nested object from an array of objects

From Dev

Get an object with null values from an array of nested objects

From Dev

Delete array from nested array

From Dev

How to create Object with nested Objects from an Array

From Dev

Delete object for a nested array (Javascript)

From Dev

Produce a nested object from array of objects with flat path

From Dev

Lodash map value from nested object in array of objects

From Dev

MongoDB: how to delete an object from a document in a nested array

From Dev

Change the nested object structure from array of objects to a common object and remove old array of objects

From Dev

Create a nested array of object from an array of objects?

From Dev

Delete object or nested object from array

From Dev

Remove matched object from deeply nested array of objects

From Dev

Create an array of objects from a nested object

From Dev

Find object from an array of nested objects by key in JavaScript

From Dev

how i can make an array from an object if there are nested objects

From Dev

getting unique objects from array of nested object and sub properties

From Dev

Updating multiple attributes of a nested state array of objects from a dynamic object

From Dev

Create nested object of booleans, arrays and objects from array

From Dev

build simple object from deeply nested array of objects

From Dev

Mongoose to get the exact object from a nested array of objects

From Dev

generic function to create an array of nested objects from a single object of arrays

From Dev

delete keys from a nested object from an array of keys

Related Related

  1. 1

    How to generate a nested array of objects from an source json object

  2. 2

    Delete last object from the array of objects.

  3. 3

    removing object from nested array of objects mongodb

  4. 4

    Delete an object from nested array in openjson SQL Server 2016

  5. 5

    Create nested object from array of objects in JavaScript

  6. 6

    Create new object from array of nested arrays that contains objects

  7. 7

    How delete an object based on its index from a nested array in React?

  8. 8

    Remove a specific nested object from an array of objects

  9. 9

    Get an object with null values from an array of nested objects

  10. 10

    Delete array from nested array

  11. 11

    How to create Object with nested Objects from an Array

  12. 12

    Delete object for a nested array (Javascript)

  13. 13

    Produce a nested object from array of objects with flat path

  14. 14

    Lodash map value from nested object in array of objects

  15. 15

    MongoDB: how to delete an object from a document in a nested array

  16. 16

    Change the nested object structure from array of objects to a common object and remove old array of objects

  17. 17

    Create a nested array of object from an array of objects?

  18. 18

    Delete object or nested object from array

  19. 19

    Remove matched object from deeply nested array of objects

  20. 20

    Create an array of objects from a nested object

  21. 21

    Find object from an array of nested objects by key in JavaScript

  22. 22

    how i can make an array from an object if there are nested objects

  23. 23

    getting unique objects from array of nested object and sub properties

  24. 24

    Updating multiple attributes of a nested state array of objects from a dynamic object

  25. 25

    Create nested object of booleans, arrays and objects from array

  26. 26

    build simple object from deeply nested array of objects

  27. 27

    Mongoose to get the exact object from a nested array of objects

  28. 28

    generic function to create an array of nested objects from a single object of arrays

  29. 29

    delete keys from a nested object from an array of keys

HotTag

Archive