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.
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.