0

I have an array of object and it looks like this, I want to remove attributeType object and get a new one. How can I delete object in array?

{
    "id": 2,
    "version": 1,
    "name": "test",
    "attributeInputs": {
        "0": {
            "id": 4,
            "position": 0,
            "label": "display",
            "attributeType": {
                "id": 3,
                "label": "Input",
                "fieldType": "TEXT"
            },
        },
        "1": {
            "id": 5,
            "position": 3,
            "label": "price",
            "attributeType": {
                "id": 5,
                "label": "Price",
                "fieldType": "PRICE"
            },
        }
    }
}

I would like delete AttributeType and it should look like this:

{
    "id": 2,
    "version": 1,
    "name": "test",
    "attributeInputs": {
        "0": {
            "id": 4,
            "position": 0,
            "label": "display",
        },
        "1": {
            "id": 5,
            "position": 3,
            "label": "price",
        }
    }
}
2

2 Answers 2

1

You can use Object.keys() on the attributeInputs field and delete the key attributeType:

const obj = {
    "id": 2,
    "version": 1,
    "name": "test",
    "attributeInputs": {
        "0": {
            "id": 4,
            "position": 0,
            "label": "display",
            "attributeType": {
                "id": 3,
                "label": "Input",
                "fieldType": "TEXT"
            },
        },
        "1": {
            "id": 5,
            "position": 3,
            "label": "price",
            "attributeType": {
                "id": 5,
                "label": "Price",
                "fieldType": "PRICE"
            },
        }
    }
}

Object.keys(obj.attributeInputs).forEach(o => delete obj.attributeInputs[o].attributeType )

console.log(obj)

Sign up to request clarification or add additional context in comments.

1 Comment

You haven't found a similar "how to delete a property of an object" question here on SO? A mystery...
0

first, let's store the object in a variable called myObject. now let's work on the variable myObject.

myObject.attributeInputs = Object.keys(myObject.attributeInputs).map(key=>{
    const {attributeType,...newObj}=myObject.attributeInputs[key];
    return newObj;
});

it's not clean code, but it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.