How do I find an array item with TypeScript? (a modern, easier way)

前端 未结 5 716
长发绾君心
长发绾君心 2020-11-28 09:49

Is there a canonical way to find an item in an array with TypeScript?

ES6+ allows this simple/clean approach

[{"id":1}, {"id":-2}         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 10:22

    You could just use underscore library.

    Install it:

       npm install underscore --save
       npm install @types/underscore --save-dev
    

    Import it

       import _ = require('underscore');
    

    Use it

        var x = _.filter(
          [{ "id": 1 }, { "id": -2 }, { "id": 3 }],
          myObj => myObj.id < 0)
        );
    

提交回复
热议问题