Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove key data from all rows from json array using javascript

This is a JSON array. I want to remove the productID from all the rows.
when I console log I don’t want to see the productID in it.

"items": 
            [
                { 
                    "productID": "11234567",
                    "added": "TIMESTAMP",
                    "title": "Project",
                    "type": "Weekend Project",
                    "imageURL": "1"
                },

                { 
                    "productID": "11223456",
                    "added": "TIMESTAMP",
                    "title": "Bathroom",
                    "type": "Weekend Project",
                    "imageURL": "2"
                },

                { 
                    "productID": "11223345",
                    "added": "TIMESTAMP",
                    "title": "Curves",
                    "type": "Collections",
                    "imageURL": "3"
                }
            ]

The issue was I faced :

I needed to export the array in excel format. I got a simple function for it. but I have to remove some contents from the original JSON which is coming from the backend.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i checked lots of threads in StackOverflow but i cant find the correct solution.

>Solution :

assuming

const items =
            [
                { 
                    "productID": "11234567",
                    "added": "TIMESTAMP",
                    "title": "Project",
                    "type": "Weekend Project",
                    "imageURL": "1"
                },

                { 
                    "productID": "11223456",
                    "added": "TIMESTAMP",
                    "title": "Bathroom",
                    "type": "Weekend Project",
                    "imageURL": "2"
                },

                { 
                    "productID": "11223345",
                    "added": "TIMESTAMP",
                    "title": "Curves",
                    "type": "Collections",
                    "imageURL": "3"
                }
            ]

You can just iterate through all elements and delete id property:

items.forEach(o=>delete o.productID)

or map it into desired structure

const newItems = items.map((o)=>({"title":o.title,"added":o.added}))

Edit:

In first approach you don’t need to create new variable, you edit orginal one, in other one you get new object so you have to assign it to new variable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading