0

I am creating a route in node.js

and this is my code:

schedule.get('/conference/schedule_participants/:circle/:schedId', function(req, res) {
  if(req.schedId){
      getParticipants( req.params, function(contacts){
        results.contacts=contacts;
          res.json(contacts);
      }); 

  }else{
        res.json(contacts);
  }
});

the output in my browser is like this :

[
{
"id":212,
"extenrealname":"UID1",
"name":"0090001513",
},
{
"id":214,
"extenrealname":"UID3",
"name":"0090001515",
},
]

How can i get name element so that my output will be like:

 [
    {
    "name":"0090001513",
    },
    {
    "name":"0090001515",
    },
    ]

Thanks :)

2 Answers 2

2

try using map to transform the input array to required array

var arr = [
    {
        "id": 212,
        "extenrealname": "UID1",
        "name": "0090001513"
    }, {
        "id": 214,
        "extenrealname": "UID3",
        "name": "0090001515"
    }
];
var output = arr.map( function(value){ return { name: value.name }; } );
console.log(output);
Sign up to request clarification or add additional context in comments.

Comments

2

Use map:

schedule.get('/conference/schedule_participants/:circle/:schedId', function(req, res) {
  if(req.schedId){
      getParticipants( req.params, function(contacts){
          res.json(contacts.map(function(c) {
            return c.name;
          });
      }); 

  }else{
        res.json(contacts.map(function(c){ return c.name}));
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.