1

I have a multidimensional array, like this:

[
    [ 
        { name: "jeff", job: "cleaner" }, 
        { name: "kate", job: "cashier" } 
    ],
    [ 
        { name: "alice", job: "engineer" },
        { name: "sean", job: "writer" },
        { name: "ruby", job: "police officer" }
    ],
    [ 
        { name: "sarah", job: "writer" }, 
        { name: "john", job: "cleaner" } 
    ]
]

I'm looking for a fast and concise way to find an object within this structure where one of the properties matches a certain value and then remove that object entirely.

For example, say I want to find all objects where the property of job is writer and remove those objects from the structure.

Please bare in mind that I would like keep the current structure of this array completely intact, with just the relevant objects removed.

shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
  • [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) is your friend, though it does not edit in place - otherwise check [`_.remove`](https://lodash.com/docs#remove), to be iterated on every entry of the outermost array. – moonwave99 May 17 '15 at 18:26
  • What is the purpose of the outer array? Will the relevant inner array always be at [0]? – Dylan Watt May 17 '15 at 18:27
  • @DylanWatt The structure represents rows and columns. And yes, all the inner arrays will always be at that level - the structure will not get any deeper. – shrewdbeans May 17 '15 at 18:30
  • Seems simple enough. Where are you stuck? –  May 17 '15 at 18:31
  • If you want to check the entire array, you're going to have to loop touching every object. Dare I mention the double-for-loop. There's **LOTS** of ways to do it, but the best solution depends on your specific situation. How big are these arrays? Are you able to coerce / retrieve the data in any other structure? – Alex McMillan May 17 '15 at 18:35
  • Thanks Alex, the arrays aren't much bigger than the example in the question, so I imagine looping isn't a big deal. Also I'm not able to retreive the data in any other structure. – shrewdbeans May 17 '15 at 18:46
  • Ok if you're talking < 1000 nodes, the double-for loop is your best bet. Remember - Javascript is **LIGHTNING** fast when you don't touch the DOM. – Alex McMillan May 17 '15 at 18:48
  • What would you recommend for > 1000 nodes? – shrewdbeans May 17 '15 at 18:57
  • 1
    @shrewdbeans If you have enough nodes that you need to consider performance, you would want to look into maintaining a lookup map. {cleaner:[], writer:[]} etc. That would allow fast lookup of each job, and the entities that contain it. However, how the lookup map is created/maintained is still an issue. In general, you have to get pretty large to need to do this. Javascript is amazing at arrays, I've dealt with 1 million+ sized arrays client side, and it handled it like a champ on modern CPU/browsers. – Dylan Watt May 17 '15 at 19:23

1 Answers1

2

I used a hasOwnProperty function which I do not take credit for from this question.

You can do it like this, using that function:

arr.forEach(function (subArr, index, myArr) {
    myArr[index] = subArr.filter(function (obj) {
        if (hasOwnProperty(obj, "job") && obj["job"] === "writer") {
            return false;
        }
        return true;
    });
});

Fiddle

Community
  • 1
  • 1
Omri Aharon
  • 16,959
  • 5
  • 40
  • 58