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

5
  • The string you posted is not valid JSON. Commented Apr 11, 2015 at 14:08
  • @Pointy updated my question, i do not want to delete, above code is just sample code. Commented Apr 11, 2015 at 14:12
  • @thefourtheye sorry updated my question. Commented Apr 11, 2015 at 14:12
  • What do you mean, "just sample code"? If you can't pose a clear question how do you expect to get help? The only difference between the (inexplicably edited) lists above is that the objects in the second one are missing two attributes present in the first one. Commented Apr 11, 2015 at 14:13
  • @Pointy just don't use delete because I need only few field, delete is bad in my case. Commented Apr 11, 2015 at 14:13

3 Answers 3

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
       };
    })
}
Sign up to request clarification or add additional context in comments.

9 Comments

you don't need "" for result?
@JamesLemon no, you don't need quotes for keys.
what if I just want array? discard the result key?
@JamesLemon then just use var output = input.result.map(function(item) { ... });
@JamesLemon if you don't use key then you can't use object it will work, check my comment.
|
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);

Comments

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'];
})

1 Comment

simple yet working solution, just nice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.