-2

From this json arrays

{
    "result": [
        {
            "id": "1",
            "name": "John",
            "type": "B",
            "score":"passed"
        },
        {
            "id": "2",
            "name": "Alice",
            "type": "A",
            "score":"failed"
        }

    ]
}

How to split out some field and turn it intosomething like this

{
    "result": [
        {
            "id": "1",
            "type": "B",
        },
        {
            "id": "2",
            "type": "A",
        }

    ]
}

I do not want to use splice in my case, above is just sample code.

James Lemon
  • 426
  • 5
  • 17

3 Answers3

4

Try this:

var input = {
    "result": [
        {
            "id": "1",
            "name": "John",
            "type": "B",
            "score":"passed"
        },
        {
            "id": "2",
            "name": "Alice",
            "type": "A",
            "score":"failed"
        }

    ]
};
var output = {
    result: input.result.map(function(item) {
       return {
          id: item.id,
          type: item.type
       };
    })
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
3

Try like this

   var json = {
   "result": [{
           "id": "1",
           "name": "John",
           "type": "B",
           "score": "passed"
       }, {
           "id": "2",
           "name": "Alice",
           "type": "A",
           "score": "failed"
       }

   ]
   };

   json.result.forEach(function(item) {
   delete item.name;
   delete item.score;
   });

   console.log(json);
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
3

iterate over arry and remove age property

var json = [
    {"name":"john",
     "age":"30",
     "gender":"male"},
    {"name":"Alice",
     "age":"20",
     "gender":"female"}

];
json.forEach(function(x){
delete x['age'];
})
Omar Elawady
  • 3,300
  • 1
  • 13
  • 17