0

I do have an array of objects and I want to create a filter function to return an new array

var items = [{
        "row": 0,
        "column": 0,
        "items": [2, 3, 4, 5, 6, 7]
    }, {
        "row": 0,
        "column": 1,
        "items": [8, 9, 10, 11, 12, 13]
            ....



        {
            "row": 2,
            "column": 2,
            "items": [50, 51, 52, 53, 54, 55]
        }]

    var newArray = function (items, row) {
        //filter items and return new array

        return filtered
    }

newArray should contain all values from 'items' that have the same row value.

7
  • Hey, welcome to StackOverflow. It would help if you explained what you have tried so far and why it didn't work for you. Commented Dec 5, 2013 at 13:46
  • Tried to group them with var rowGrps = _.groupBy(items, function(p){ return p.row; }); but didn't feel right Commented Dec 5, 2013 at 13:48
  • Could you demonstrate the result you're expecting to be created from this? Commented Dec 5, 2013 at 13:50
  • 1
    @lunacafu: But that's just what you want? How else should the expected output look like? Btw, you could shorten that to _.groupBy(items, "row") Commented Dec 5, 2013 at 13:50
  • Question is confusing because the top-level array is called items, but also each item contains a property named items Commented Dec 5, 2013 at 13:55

3 Answers 3

3

If I understand the question correctly, the result would be given by

function filter(items, row) {
    return _.chain(items)    // initiate method chaining for convenience
        .where({row: row})   // filter out objects from rows we don't care about
        .pluck("items")      // get the "items" arrays from the filtered objects
        .flatten()           // concatenate them into a single array
        .value();            // unwrap the result to return it
}

Calling filter(items, 0) in the example given would return

[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

which is the concatenated aggregate of items arrays inside objects with row equal to 0.

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

1 Comment

This is exactly what I was looking for, it returns a plain array with all the items values from all objects that match row id
2

Maybe you're looking for this:

_.reduce(items, function(res, item) {
    var key = item.row;
    if (!(key in res)) res[key] = [];
    [].push.apply(res[key], item.items);
    return res;
}, {})

1 Comment

also feasible, returns an object with all items as separate arrays
1

In underscore, to get all items where the row value matches the index of the item in the array:

var items = [...];
var filteredItems = _.filter(items, function(item, i) {
  return item.row == i;
});

Or with the native Array.prototype.map method. E.g.

var items = [...];
var filteredItems = items.filter(function(item, i) {
  return item.row == i;
});

2 Comments

the result I receive contains only the first object with row 0
Of course, since this does only return items whose row value is the same as their index in the items array…

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.