0

i have an array of objects

const arr=[
    {
        "id": "C3-Delta-65",
        "name": "C3-Delta-65",
        "carts": [
            {
                "id": "full",
                "name": "Full Cart",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 5
            },
            {
                "id": "half",
                "name": "Half Cart",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 3
            },
            {
                "id": "smu",
                "name": "SMU",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 2
            }
        ]
    },
    {
        "name": "C3-Delta-65",
        "id": "C3-Delta-65",
        "carts": [
            {
                "id": "full",
                "name": "Full Cart",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 5
            },
            {
                "id": "half",
                "name": "Half Cart",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 3
            },
            {
                "id": "smu",
                "name": "SMU",
                "weight": 0,
                "volume": 0,
                "dimensions": {
                    "length": 0,
                    "breadth": 0,
                    "height": 0
                },
                "quantity": 2
            }
        ]
    },
    {
        "name": "B1-Bravo-31",
        "id": "B1-Bravo-31",
        "carts": [
            {
                "quantity": 3,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "Full Cart",
                "id": "full"
            },
            {
                "quantity": 4,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "Half Cart",
                "id": "half"
            },
            {
                "quantity": 2,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "SMU",
                "id": "smu"
            }
        ]
    }
]

i want to remove the object rem from the arr array only once right now there are two rem objects which are exactly the same as rem object. i need the whole rem object to be compared and deleted from arr array instead of using a key from rem to be compare with arr array is this possible.

   const rem={
        "name": "C3-Delta-65",
        "id": "C3-Delta-65",
        "carts": [
            {
                "quantity": 5,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "Full Cart",
                "id": "full"
            },
            {
                "quantity": 3,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "Half Cart",
                "id": "half"
            },
            {
                "quantity": 2,
                "dimensions": {
                    "height": 0,
                    "breadth": 0,
                    "length": 0
                },
                "volume": 0,
                "weight": 0,
                "name": "SMU",
                "id": "smu"
            }
        ]
    }
bugsmasher
  • 153
  • 6
  • Does this answer your question? [remove objects from array by object property](https://stackoverflow.com/questions/16491758/remove-objects-from-array-by-object-property) – bonCodigo May 07 '23 at 05:15

1 Answers1

0

Yes it is possible.

You will have find the index in the array using indexOf, and then splice.

To test for equality, you can use JSON.stringify, compare shallow properties using a loop and Object.entries, or use a deepEquals library function.

The order of your properties may or may not be important depending on your approach.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78