用lodash

时间:2016-11-23 11:54:16

标签: javascript lodash

我有学校收藏,学校有教师收藏,每位老师都有学生收藏。每个学生都有全球学生UniqueId。

我想通过使用lodash找到有学生的学校,其中studentUniqueId是:“Abc123946”。它可以在循环中完成,但我想像在linq中那样优雅地完成它。

示例数据在这里:

https://jsfiddle.net/94f4wvb6/

[  
   {  
  "schoolId":1,
  "schoolName":"school name 1",
  "teachers":[  
     {  
        "name":"Teacher Name 1",
        "subject":"Math",
        "students":[  
           {  
              "studentUniqueId":"Abc123940",
              "name":"Student Name 1"
           },
           {  
              "studentUniqueId":"Abc123941",
              "name":"Student Name 1"
           }
        ]
     },
     {  
        "name":"Teacher Name 2",
        "subject":"English",
        "students":[  
           {  
              "studentUniqueId":"Abc123942",
              "name":"Student Name 1"
           },
           {  
              "studentUniqueId":"Abc123943",
              "name":"Student Name 1"
           }
        ]
     }
  ]
},
{  
 "schoolId":2,
  "schoolName":"school name 2",
  "teachers":[  
     {  
        "name":"Teacher Name 3",
        "subject":"Math",
        "students":[  
           {  
              "studentUniqueId":"Abc123944",
              "name":"Student Name 7"
           },
           {  
              "studentUniqueId":"Abc123945",
              "name":"Student Name 8"
           }
        ]
     },
     {  
        "name":"Teacher Name 4",
        "subject":"English",
        "students":[  
           {  
              "studentUniqueId":"Abc123946",
              "name":"Student Name 5"
           },
           {  
              "studentUniqueId":"Abc123947",
              "name":"Student Name 6"
           }
        ]
     }
  ]
 }
]

2 个答案:

答案 0 :(得分:1)

以下是使用findsome执行此操作的一种方法:

let theOneThatIWant = "Abc123942";

let school = _.find(schools, function(school){
    return _.some(school.teachers, function(teacher){
        return _.some(teacher.students, {studentUniqueId: theOneThatIWant});
    })
})

答案 1 :(得分:1)

using (var inputStream = new FileStream("file.in", FileMode.Open))
using (var outputStream = new FileStream("file.out", FileMode.Create))
{
    using (var compressor = new DeflateStream(outputStream, Ionic.Zlib.CompressionMode.Compress))
    {
        byte[] buffer = new byte[4096];
        int n;
        while ((n = inputStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            compressor.FlushMode = FlushType.Sync;
            compressor.Write(buffer, 0, n);
            compressor.Flush();
            // actually, Flush() is not needed, DeflateStream automatically flushes on every write.
        }
    }
}