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 values in array of objects

I am having a array of objects which looks like this

var data = [
    {
        "id": "K014-s1",
        "status": true,
        "amount": 992,
        "check": true,
      
    },
    {
        "id": "K014-s2",
        "status": false,
        "amount": 10992,
        "check": true,
       
    }
]

I want only certain key values from the object in the array

Required Output:

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

 var data = [
        {
            "id": "K014-s1",
            "amount": 992,
        },
        {
            "id": "K014-s2",
            "amount": 10992,
        }
    ]

Code I tried:

     var filteredData = []
        var result = data.map((obj) => {
        filteredData.push(obj.id)
        })

console.log(filteredData)

I tried. But don’t Know how to make it. Please Help me with some solutions

>Solution :

Array.prototype.map already creates a new array, so result will already be the new value you are looking for.

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

var filteredResult = data.map((obj) => {
   //additional logic, if needed here.
   return {
      id: obj.id,
      amount: ob.amount,
   }        
})

Alternatively you can of course use a for loop or array.prototype.forEach to achieve the same:

var filteredData = []
data.forEach((obj) => {
  filteredData.push({
    id: obj.id,
    amount: ob.amount,
  })
})
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