0

I have below json which has a node careerLevels and this contains inner child elments.

input = {
            "careerLevelGroups": [
                {
                    "201801": 58,
                    "201802": 74,
                    "careerLevel": "Analyst",
                    "careerLevels": [
                        {
                            "201801": 29,
                            "201802": 37,
                            "careerID": "10000100"
                        },
                        {

                            "201801": 29,
                            "201802": 37,
                            "careerID": "10000110"
                        }
                    ]
                },
                {

                    "201801": 58,
                    "201802": 74,
                    "careerLevel": "Consultant",
                    "careerLevels": [
                        {

                            "201801": 29,
                            "201802": 37,
                            "careerID": "10000080"
                        },
                        {

                            "201801": 29,
                            "201802": 37,
                            "careerID": "10000090"
                        }
                    ]
                }
]}

i need to remove all the careerLevels node dynamically from this json and only display below json.

output= {
            "careerLevelGroups": [
                {
                    "201801": 58,
                    "201802": 74,
                    "careerLevel": "Analyst"

                },
                {

                    "201801": 58,
                    "201802": 74,
                    "careerLevel": "Consultant"

                }
]}

I have tried to use

let strippedJson = copyObject(mergedJson);
    delete strippedJson.careerLevels; // remove careerLevels but this is not doing anything.

I think i need to use something like :

input.forEach(element => {element.delete}) // something like this
1
  • I answered, hope it will work as per the expectation. Thanks. Commented Mar 30, 2018 at 6:51

2 Answers 2

2

You can try following code:

let output = JSON.parse(JSON.stringify(input));
output.careerLevelGroups.forEach(group => delete group.careerLevels);
Sign up to request clarification or add additional context in comments.

Comments

1

Use array Map method to iterate the careerLevelGroups array and then delete the careerLevels property of an object :

var jsonObj = {
	"careerLevelGroups": [{
			"201801": 58,
			"201802": 74,
			"careerLevel": "Analyst",
			"careerLevels": [{
					"201801": 29,
					"201802": 37,
					"careerID": "10000100"
				},
				{

					"201801": 29,
					"201802": 37,
					"careerID": "10000110"
				}
			]
		},
		{

			"201801": 58,
			"201802": 74,
			"careerLevel": "Consultant",
			"careerLevels": [{

					"201801": 29,
					"201802": 37,
					"careerID": "10000080"
				},
				{

					"201801": 29,
					"201802": 37,
					"careerID": "10000090"
				}
			]
		}
	]
};

var res = jsonObj.careerLevelGroups.map(obj => {
delete obj.careerLevels
return obj;
});

console.log(res);

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.