0

I am getting a JSON response like below:

[  
{
"id": "1",
"name": "Abhi",
"pan": "ABC",
"bg": "O+"  
},  
{
"id": "2",
"name": "Ashish",
"pan": "XYZ",
"bg": "AB+"  
},
.
.
.
]

and I want to remove the "pan" and "bg" field from all elements of the array in the final response.Like below:

[
{
"id": "1",
"name": "Abhi"
},
{
"id": "2",
"name": "Ashish"
},
.
.
.
]
0

1 Answer 1

2

Try using map function.

const data = [
{"id": "1", "name": "Abhi", "pan": "ABC", "bg": "O+"},  
{"id": "2", "name": "Ashish", "pan": "XYZ", "bg": "AB+"}
]
const newArr = data.map(item => { return {
  id: item.id,
  name: item.name
}})
console.log(newArr)

map function always generate a new array. You can go for forEach and delete method to update the actual array.

const data = [
  {"id": "1", "name": "Abhi", "pan": "ABC", "bg": "O+" },  
  {"id": "2", "name": "Ashish", "pan": "XYZ", "bg": "AB+"}
];

data.forEach((item) => { delete item.pan; delete item.bg });

console.log(data);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.