skip to Main Content

In the following object called ‘task’, the door property could arbitrary each time:

var task = {};

task["123"] = [{"door":""}, {"door":""},  {"door":""}, {"door":""}];
task["456"] = [{"door":""}, {"door":"close"},  {"door":""}, {"door":""}];
task["789"] = [{"door":""}, {"door":""},  {"door":""}, {"door":""}];

Let’s say we need to find if there is an entry in the task object which has an array item with a property not equal to "". So in the above, there is only one such entry and its value is "close".

One could use nested for loops which bail out as soon as a non empty value is found. How else could one do it? (maybe using LINQ to create shorter code?)

2

Answers


  1. LINQ belongs to C# not JS. But you can use ES6 syntax e. g. a .some() after a .flat() if you want to detect if there’s a door !== "":

    var task = {};
    task["123"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    task["456"] = [{door: ""}, {door: "close"}, {door: ""}, {door: ""}];
    task["789"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    
    const hasNonEmptyDoor = Object.values(task)
      .flat()
      .some(item => item.door !== "");
    
    console.log(hasNonEmptyDoor);
    Login or Signup to reply.
  2. Could be done with several Object::values and Array::some in a chain:

    var task = {};
    task["123"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    task["456"] = [{door: ""}, {door: "close"}, {door: ""}, {door: ""}];
    task["789"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    
    const hasNonEmptyProp = Object.values(task).some(task => task.some(task => Object.values(task).some(val => val !== '')));
    
    console.log(hasNonEmptyProp);

    One crazy way would be to use a regex if you need the smallest code. It’s possible to regex only the arrays which is actually faster than flatting the whole object:

    var task = {};
    task["123"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    task["456"] = [{door: ""}, {door: "close"}, {door: ""}, {door: ""}];
    task["789"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    
    const hasNonEmptyProp = /":[^"]+"/.test(JSON.stringify(task));
    
    console.log(hasNonEmptyProp);

    And a benchmark:

    enter image description here

    <script benchmark data-count="200000">
    
    var task = {};
    task["123"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    task["456"] = [{door: ""}, {door: "close"}, {door: ""}, {door: ""}];
    task["789"] = [{door: ""}, {door: ""}, {door: ""}, {door: ""}];
    
    // @benchmark Behemoth
    Object.values(task)
      .flat()
      .some(item => item.door !== "");
      
    // @benchmark Alexander
    Object.values(task).some(task => task.some(task => Object.values(task).some(val => val !== '')));
    
    // @benchmark regex
    Object.values(task).some(task => /"[^"]+"/.test(JSON.stringify(task)));
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search