CHY
CHY

Reputation: 87

How do I remove an element from a JSON object by key reference?

I chose this JSON structure to store some data because I thought it would be easy to reference each object by it's key but I can't seem to correctly remove an element by key.

JSON Object:

[{
    "a74bb26c-c23f-e711-80da-0050568f4ab2": {
        "VersionCode": "0ODD6109",
        "VersionQualifier": "",
        "ProductID": "WRAP",
    }
}, {
    "a74fff6c-c23f-e711-80da-0050568f4ab2": {
        "VersionCode": "0ODD6109",
        "VersionQualifier": "",
        "ProductID": "WRAP",
    }
}]

When

key="a74fff6c-c23f-e711-80da-0050568f4ab2"

and

dataObj = {
            "VersionCode": "0ODD6109",
            "VersionQualifier": "",
            "ProductID": "WRAP",
           }

I can easily add items using:

dataObject = an object with properties
newItem = {}
newItem[key] = dataObj
myJsonObj.push(newItem).

How can I remove an item when I have the key as a variable?

This hasn't worked:

delete myJsonObj[key]

This also hasn't worked:

var index = myJsonObj.indexOf(node, 0);
if (index > -1) {
     myJsonObj.splice(index, 1);
}

Upvotes: 1

Views: 722

Answers (3)

terales
terales

Reputation: 3200

Your JSON object is wrongly constructed. It should be like this:

{
  "a74bb26c-c23f-e711-80da-0050568f4ab2": {
      "VersionCode": "0ODD6109",
      "VersionQualifier": "",
      "ProductID": "WRAP"
  },
  "a74fff6c-c23f-e711-80da-0050568f4ab2-unique": {
        "VersionCode": "0ODD6109",
        "VersionQualifier": "",
        "ProductID": "WRAP"
  }
}

Notice removed parensis.

You can add items like this:

dataObject = an_object_with_properties;
myJsonObj[key] = dataObj

After this change your example would work perfectly:

delete myJsonObj[key]

Here is an example:

const dataObject1 = {
  "VersionCode": "0ODD6109",
  "VersionQualifier": "",
  "ProductID": "WRAP",
}

const dataObject2 = {
  "VersionCode": "0ODD6109",
  "VersionQualifier": "",
  "ProductID": "WRAP",
}

const myJsonObj = {}

myJsonObj['key1'] = dataObject1
myJsonObj['key2'] = dataObject2

console.log('Before delete', myJsonObj)

delete myJsonObj['key1']

console.log('After delete', myJsonObj)

Upvotes: 0

raneshu
raneshu

Reputation: 413

Your json is wrongly formatted. You should remove the comma after ProductID:"WRAP", <--- this comma. Here is valid json:

[{
    "a74bb26c-c23f-e711-80da-0050568f4ab2": {
        "VersionCode": "0ODD6109",
        "VersionQualifier": "",
        "ProductID": "WRAP"
    }
}, {
    "a74fff6c-c23f-e711-80da-0050568f4ab2": {
        "VersionCode": "0ODD6109",
        "VersionQualifier": "",
        "ProductID": "WRAP"
    }
}]

To remove an element. Just use: JSONArray = JSONArray.filter(obj => !obj[keyToBeFilteredOut]);

Upvotes: 1

CHY
CHY

Reputation: 87

Found a solution but I am curious to hear if there are others or if there is a better structure for my array to begin with:

newJsonObj = myJsonObj.filter(function (el) { return !el[key]; });

Upvotes: 0

Related Questions