lodash过滤器,如果两个值都出现在对象中

时间:2016-04-14 16:04:18

标签: lodash

如果仅显示代码和日期时间,如何过滤。下面的案例代码不正确,因为它还显示没有代码的日期时间。

最后,我如何将输出转换为php变量,以便我可以将其插入mysql

    <script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.min.js"></script>
<script>
    var data = <?php echo json_encode($data); ?>;   
    var users =_.map(data, function(obj) {
                return _.pick(obj, 'code','datetime');
            });

_.forEach(users, function(value, key) {
  console.log(value);
});
</script>

下面的示例输出。

Object {datetime: "2016-04-11T08:49:24Z"}
    u.php:10 Object {datetime: "2016-04-11T08:48:13Z"}
    u.php:10 Object {datetime: "2016-04-11T08:47:44Z"}
    u.php:10 Object {code: "7592584881", datetime: "2016-04-11T08:46:55Z"}
    u.php:10 Object {datetime: "2016-04-11T08:45:21Z"}
    u.php:10 Object {datetime: "2016-04-11T08:44:28Z"}
    u.php:10 Object {datetime: "2016-04-11T08:39:14Z"}
    u.php:10 Object {datetime: "2016-04-11T08:44:27Z"}
    u.php:10 Object {datetime: "2016-04-11T08:38:41Z"}
    u.php:10 Object {datetime: "2016-04-11T08:36:51Z"}
    u.php:10 Object {code: "7830179866", datetime: "2016-04-11T08:42:23Z"}
    u.php:10 Object {datetime: "2016-04-11T08:42:02Z"}
    u.php:10 Object {datetime: "2016-04-11T08:41:49Z"}
    u.php:10 Object {datetime: "2016-04-11T08:33:54Z"}

这是$ data的json数组

[
    {
        "_id": "570e4f4ee4b0325dbf42c6c8",
        "ap_from": "44:d9:e7:7a:63:81",
        "ap_to": "44:d9:e7:7a:60:62",
        "channel": "1",
        "channel_from": "1",
        "channel_to": "1",
        "datetime": "2016-04-13T13:52:49Z",
        "guest": "e8:50:8b:a7:84:60",
        "key": "EVT_WG_Roam",
        "msg": "Guest[e8:50:8b:a7:84:60] roams from AP[44:d9:e7:7a:63:81] to AP[44:d9:e7:7a:60:62] from \"channel 1(ng)\" to \"channel 1(ng)\" on \"EandCCopraWireless\"",
        "radio": "ng",
        "radio_from": "ng",
        "radio_to": "ng",
        "site_id": "565eedd4e4b077dd32e114d0",
        "ssid": "EandCCopraWireless",
        "subsystem": "wlan",
        "time": 1460555569274
    },
    {
        "_id": "570e4e89e4b0325dbf42c6c0",
        "ap": "44:d9:e7:7a:63:81",
        "channel": "1",
        "datetime": "2016-04-13T13:49:37Z",
        "guest": "00:73:8d:8d:e7:58",
        "key": "EVT_WG_Connected",
        "msg": "Guest[00:73:8d:8d:e7:58] has connected to AP[44:d9:e7:7a:63:81] with ssid \"EandCCopraWireless\" on \"channel 1(ng)\"",
        "radio": "ng",
        "site_id": "565eedd4e4b077dd32e114d0",
        "ssid": "EandCCopraWireless",
        "subsystem": "wlan",
        "time": 1460555377077
    },
{
        "_id": "570e3eebe4b0325dbf42c603",
        "code": "8516695243",
        "datetime": "2016-04-13T12:43:23Z",
        "guest": "82:81:95:74:77:90",
        "key": "EVT_HS_VoucherUsed",
        "msg": "Voucher[8516695243] was used by Guest[82:81:95:74:77:90]",
        "site_id": "565eedd4e4b077dd32e114d0",
        "subsystem": "wlan",
        "time": 1460551403094
    },
.....
.....
.....

1 个答案:

答案 0 :(得分:0)

您应该先将data数组中不需要的元素过滤掉,然后再将其传递给_.map()

var users = _.map(
    _.filter(data, function (obj) {
        return _.has(obj, 'code') && _.has(obj, 'datetime');
    }),
    function (obj) {
        return _.pick(obj, ['code', 'datetime']);
    }
);