如何根据 JavaScript 对象中的状态值过滤记录?
How to filter records based on the status value in JavaScript object?
我有如下所示的 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
}
]
如您所见,数据集是一个数组,我想在应用过滤器之前将我的数据保存在与第一个示例中的对象相同的对象中。有没有一种方法可以过滤对象并实现与过滤数组相同的输出?
试试这个:
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);
您可以使用 .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
如果您使用的是过滤数组函数,它returns是一个简单的数组,而不是像原来那样的对象(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;
}
}
}
这就是您要查找的内容:
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);
对每个项目调用谓词,如果 returns 为真,则该项目包含在结果中。
我说的是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
对象。
我有如下所示的 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
}
]
如您所见,数据集是一个数组,我想在应用过滤器之前将我的数据保存在与第一个示例中的对象相同的对象中。有没有一种方法可以过滤对象并实现与过滤数组相同的输出?
试试这个:
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);
您可以使用 .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
如果您使用的是过滤数组函数,它returns是一个简单的数组,而不是像原来那样的对象(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;
}
}
}
这就是您要查找的内容:
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);
对每个项目调用谓词,如果 returns 为真,则该项目包含在结果中。
我说的是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
对象。