如何根据JavaScript对象中的状态值过滤记录?

时间:2018-08-01 02:37:35

标签: javascript arrays json filter jsobject

我有一个看起来像这样的JS对象:

{
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "lmorgan@gmail.com",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "dhart@gmail.com",
    "phone": "(509) 874-9411",
    "status": 1
  }
}

我想过滤对象,例如仅提取状态为“ 1”的记录。一种解决方案是使用像这样的数组过滤器:

var filterJSON = Object.values(obj).filter(function (entry) {
                switch(frmFilter){
                    case '1':
                        return entry.status === 1;
                        break;
                    case '2':
                        return entry.status === 0;
                        break;
                    default:
                        return entry;
                }
            });

问题是上面的代码会将数据转换为数组,如下所示:

[
  {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "lmorgan@gmail.com",
    "phone": "(508) 233-8908",
    "status": 1
  },
  {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "dhart@gmail.com",
    "phone": "(509) 874-9411",
    "status": 1
  }
]

如您所见,数据集是一个数组,我想在应用过滤器之前将数据保留在与第一个示例相同的对象中。有没有一种方法可以过滤对象并获得与通过数组过滤相同的输出?

5 个答案:

答案 0 :(得分:0)

尝试一下:

const data = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "lmorgan@gmail.com",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "dhart@gmail.com",
    "phone": "(509) 874-9411",
    "status": 1
  }
};

const filtered = Object.values(data).filter(e => e.status == 1);
const output = {};
filtered.forEach(e => output[e.id] = e);
console.log(output);

答案 1 :(得分:0)

您可以将.filter().reduce()结合使用,将过滤后的数组转换回对象:

var obj = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "lmorgan@gmail.com",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "dhart@gmail.com",
    "phone": "(509) 874-9411",
    "status": 1
  }
}

var frmFilter = "1";

var filterJSON = Object.keys(obj).filter(function (key) {
    let entry = obj[key];
    switch(frmFilter){
        case '1':
            return entry.status === 1;
            break;
        case '2':
            return entry.status === 0;
            break;
        default:
            return entry;
    }
}).reduce( (res, key) => (res[key] = obj[key], res), {} );

console.log(filterJSON);

此答案提供了一些帮助:JavaScript: filter() for Objects

答案 2 :(得分:0)

如果您使用过滤器Array函数,它将返回一个简单的数组,而不是原始(obj)之类的对象

您可以在每个对象中执行简单的for-each复制和修改对象属性:

var filterJSON = {};
for (var key in obj) {
  if (obj.hasOwnProperty(key)){

        switch(frmFilter){
                case '1':
                    if (filterJSON[key].status == 1){
                       filterJSON[key] = obj[key];
                    } 
                    break;
                case '2':
                    if (filterJSON[key].status == 0){
                       filterJSON[key] = obj[key];
                    }
                    break;
         }
   }
}

答案 3 :(得分:0)

这是您要寻找的:

const filter = (data, predicate) => {
  return Object.keys(data).reduce((acc, current) => {
    if (predicate(data[current])) {
      acc[current] = data[current];
    }
    return acc;
  }, {});
}

// result contains only items with status of 1
const result = filter(data, (item) => item.status === 1);

// result2 contains only items with last name of length > 4
const result2 = filter(data, (item) => item.last.length > 4);

在每个项目上调用谓词,如果它返回true,则该项目包括在结果中。

答案 4 :(得分:0)

我告诉JS中的for...in循环,该循环用于迭代对象的属性。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

var obj = {
  "3": {
    "id": 3,
    "first": "Lisa",
    "last": "Morgan",
    "email": "lmorgan@gmail.com",
    "phone": "(508) 233-8908",
    "status": 0
  },
  "4": {
    "id": 4,
    "first": "Dave",
    "last": "Hart",
    "email": "dhart@gmail.com",
    "phone": "(509) 874-9411",
    "status": 1
  }
}
var filteredObj = {};
for (var props in obj){
  if(obj[props].status===1)
  filteredObj[props] = obj[props] ; 
  }
  
  console.log(filteredObj)

您正在遍历obj的属性并检查状态。如果status等于1,则我们将该属性分配给filteredObj对象。