js 如何取出json中符合条件的数据

这是json

[{
    "ulog_id": 5,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}, {
    "ulog_id": 4,
    "ulog_time": 2018,
    "user_name": "ceshi123"
}, {
    "ulog_id": 3,
    "ulog_time": 2017,
    "user_name": "ceshi123"
}, {
    "ulog_id": 2,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}, {
    "ulog_id": 1,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}]

如何取出所有 ulog_time为2015 的数据或者其他指定条件的数据。

阅读 7.5k
6 个回答

授人以鱼不如授人以渔,从一个对象数组中过滤出自己想要的数据是很常见的需求,不管是用filter还是其他工具库的filter亦或是自己封装个工具函数来遍历数组,核心思想都是通过创建一个新数组,循环一遍原数组来从中收集出符合条件的元素。

function filter(arr, callback, context) {
    var ret = [];
    if (Array.isArray(arr)) {
        for (var i = 0; i < arr.length; ++i) {
            var item = arr[i];
            if (callback.call(context, item, i, arr) === true) {
                ret.push(item);
            }
        }
    }
    return ret;
}

var json = [{
    "ulog_id": 5,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}, {
    "ulog_id": 4,
    "ulog_time": 2018,
    "user_name": "ceshi123"
}, {
    "ulog_id": 3,
    "ulog_time": 2017,
    "user_name": "ceshi123"
}, {
    "ulog_id": 2,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}, {
    "ulog_id": 1,
    "ulog_time": 2015,
    "user_name": "ceshi123"
}];

console.log(filter(json, function (n) { return n.ulog_time === 2015; }));
const json = [{},{},{},{}]
const result = json.filter(item => item.ulog_time === 2015)
const result = json.filter(function(item) {
    return item.ulog_time === 2015
})

filter。如果是你上面说的兼容的问题,你可以使用遍历来解决。或者Polyfill。或者使用其他类库?

var arr =[{"ulog_id":5,"ulog_time":2015,"user_name":"ceshi123"},{"ulog_id":4,"ulog_time":2018,"user_name":"ceshi123"},{"ulog_id":3,"ulog_time":2017,"user_name":"ceshi123"},{"ulog_id":2,"ulog_time":2015,"user_name":"ceshi123"},{"ulog_id":1,"ulog_time":2015,"user_name":"ceshi123"}]
for(var i = 0, il = arr.length; i < il; i++){
    var idx = il-i-1
    if(arr[idx].ulog_time != 2015) arr.splice(idx ,1)
}
console.log(arr)
if (!Array.prototype.filter){
  Array.prototype.filter = function(func, thisArg) {
    'use strict';
    if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
        throw new TypeError();
   
    var len = this.length >>> 0,
        res = new Array(len), // preallocate array
        t = this, c = 0, i = -1;
    if (thisArg === undefined){
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          if (func(t[i], i, t)){
            res[c++] = t[i];
          }
        }
      }
    }
    else{
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          if (func.call(thisArg, t[i], i, t)){
            res[c++] = t[i];
          }
        }
      }
    }
   
    res.length = c; // shrink down array to proper size
    return res;
  };
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题