1

我有看起来像这样的 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
  }
]

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

4

5 回答 5

1

您可以使用.filter()with.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

于 2018-08-01T02:44:24.787 回答
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);

于 2018-08-01T02:42:02.417 回答
0

如果您使用的是 filter 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;
         }
   }
}
于 2018-08-01T02:50:10.083 回答
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,则该项目包含在结果中。

于 2018-08-01T02:57:15.710 回答
0

我在告诉for...inJS 中用于迭代对象属性的循环。

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对象。

于 2018-08-01T03:40:53.387 回答