1

I am trying to get all IDs from an array objects and put the in a list so they can be passed to a method (API)

    var tempObj= Getlist();
    var tmpList = tempObj.listOfdata.filter(function (result) { return (result.Id) });

    var data = tmpList

then I have my AJAX call

$.ajax({
        url: url,
        data: JSON.stringify(data),
        contentType: 'application/json; charset=utf-8',
        async: true,
        method: 'POST',
        success: function (data) {
            console.log(data);       
        }
    });

no data is being passed

3
  • What type of object does your API accept? And what if you removed JSON.stringify() and just used data ? Commented Jun 14, 2017 at 14:45
  • you want map, not filter I think? Commented Jun 14, 2017 at 14:47
  • my api is accepting a list of ints which will be the id's Commented Jun 14, 2017 at 14:48

1 Answer 1

5

If you want to extract values from a collection of objects, don't use filter. Use map.

let list = [
  { id: 1 },
  { id: 3 },
  { id: 23 },
  { id: 16 }
];

let data = list.map((obj) => obj.id);
console.log(data);

In ES5:

var list = [
  { id: 1 },
  { id: 3 },
  { id: 23 },
  { id: 16 }
];

var data = list.map(function(obj) {
  return obj.id;
});
console.log(data);

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

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.