Equivalent of LINQ projection / select with Javascript

Not strictly an ‘equivilent’ but a way of achieving the same.

For example, consider the following array:

var products = [{
    id: 1,
    name: "Product One"},
{
    id: 2,
    name: "Product Two"},
{
    id: 5,
    name: "Another Product"}
];

Say, for example, we just wanted a list of IDs from this array.

With .net, using LINQ, we could do a simple Lambda:

.Select(x=> x.id)

With Javascript, we can use Map to achieve something similar:

products.map(function(product) {
    return product.id
})

I made this available as a jsFiddle for demonstration purposes:

http://jsfiddle.net/alexjamesbrown/GWqT7/

Leave a Reply

Your email address will not be published. Required fields are marked *