欢迎光临
感受代码之美

Lodash-一个JavaScript库

Lodash是一个JavaScript库,它使用函数式编程范例为常见的编程任务提供实用程序功能。下面提供了几个函数的使用示例。

1._.filter

使用说明:对数组进行过滤,只返回符合条件的元素。

var nodes = [{
            "typeUri": "object.person",
            "labelName": "人员",
            "properties": [{
                    "propertyKey": "idNumber",
                    "displayType": "C",
                    "propertyName": "身份证号",
                    "valueType": "string",
                    "propertyValue": [
                        "370188198308260386"
                    ],
                    "detail": true,
                    "queryType": "",
                    "statistics": false
                },
                {
                    "propertyKey": "name",
                    "displayType": "C",
                    "propertyName": "姓名",
                    "valueType": "string",
                    "propertyValue": [
                        "李某受贿嫌疑人"
                    ],
                    "detail": true,
                    "queryType": "",
                    "statistics": false
                },
                {
                    "propertyKey": "gender",
                    "displayType": "C",
                    "propertyName": "性别",
                    "valueType": "string",
                    "propertyValue": [
                        "男"
                    ],
                    "detail": true,
                    "queryType": "",
                    "statistics": false
                },
                {
                    "propertyKey": "birthday",
                    "displayType": "C",
                    "propertyName": "出生日期",
                    "valueType": "string",
                    "propertyValue": [
                        "1976-08-07"
                    ],
                    "detail": true,
                    "queryType": "",
                    "statistics": false
                }
            ]
        },
        {
            "typeUri": "object.car",
            "labelName": "车辆",
            "properties": [{
                "propertyKey": "carBrand",
                "displayType": "C",
                "propertyName": "车辆品牌",
                "valueType": "string",
                "propertyValue": [
                    "1111"
                ],
                "detail": true,
                "queryType": "",
                "statistics": false
            }]
        },
        {
            "typeUri": "object.car",
            "labelName": "车辆",
            "properties": [{
                "propertyKey": "carBrand",
                "displayType": "C",
                "propertyName": "车辆品牌",
                "valueType": "string",
                "propertyValue": [
                    "ccc"
                ],
                "detail": true,
                "queryType": "",
                "statistics": false
            }]
        }
    ];

// 过滤数组中,元素属性'typeUri'为'object.person'的元素。
var persons = _.filter(nodes, ['typeUri', 'object.person']);
console.log(JSON.stringify(persons));    

2._.some

使用说明:检查集合中是否有符合指定条件的元素,有则返回true。

var obj = [
  {
    "propertyKey": "name",
    "displayType": "C",
    "propertyName": "姓名",
    "valueType": "string",
    "propertyValue": [
      "齐明"
    ],
    "detail": true,
    "queryType": "",
    "statistics": false
  },
  {
    "propertyKey": "idNumber",
    "displayType": "C",
    "propertyName": "身份证号",
    "valueType": "string",
    "propertyValue": [
      "98765417263"
    ],
    "detail": true,
    "queryType": "",
    "statistics": false
  },
  {
    "propertyKey": "gender",
    "displayType": "C",
    "propertyName": "性别",
    "valueType": "string",
    "propertyValue": [
      "男"
    ],
    "detail": true,
    "queryType": "",
    "statistics": false
  },
  {
    "propertyKey": "nation",
    "displayType": "C",
    "propertyName": "民族",
    "valueType": "string",
    "propertyValue": [
      "汉"
    ],
    "detail": true,
    "queryType": "",
    "statistics": false
  },
  {
    "propertyKey": "birthday",
    "displayType": "C",
    "propertyName": "出生日期",
    "valueType": "string",
    "propertyValue": [
      "19980912"
    ],
    "detail": true,
    "queryType": "",
    "statistics": false
  }
];

// 检查数组中是否存在'propertyKey'为'idNumber'、'propertyValue'为'["98765417263"]'的元素,有则返回true。
var checkFlag = _.some(s, {"propertyKey":"idNumber","propertyValue":["98765417263"]});

参考:

  1. Lodash Documents v4.17.15
转载请注明来源:四个空格 » Lodash-一个JavaScript库

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址