使用特定值筛选数组

Filtering array with specific values

本文关键字:筛选 数组      更新时间:2023-09-26

我有这个 JSON:

{
  "1": {
    "PerId":"10900662",
    "Name":"Cueball",
    "Email":"cueb@example.com",
    "DepartId":"11"
  },
  "2": {
    "PerId":"10900664",
    "Name":"Megan",
    "MuEmail":"megan@example.com",
    "DepartId":"11"
  },
  "3": {
    "PerId":"10900665",
    "Name":"Beret Guy",
    "MuEmail":"bg@example.com",
    "DepartId":"12"
  }
}

我想用特定的DepartId过滤

这是我测试过的代码,来自这个堆栈溢出问题:

<html>
<body>
  Test!
</body>
<script>
  var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"cueb@example.com","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"megan@example.com","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"bg@example.com","DepartId":"12"}}');
  var z = y.filter(function (i,n){return n.DepartId == '11'})
</script>
</html>

在这种情况下,y 返回一个对象,但 Firefox 抛出错误y.filter is not a function.

我希望 y 会以类似

{
  "1": {
    "PerId":"10900662",
    "Name":"Cueball",
    "Email":"cueb@example.com",
    "DepartId":"11"
  },
  "2": {
    "PerId":"10900664",
    "Name":"Megan",
    "MuEmail":"megan@example.com",
    "DepartId":"11"
  }
}

以 JavaScript 对象的形式。我如何让它工作?

filter()是在Array原型上定义的。它不能在对象上使用。

您可以使用Object.keys()获取对象中的所有键,并for循环来迭代它们。

// Get all keys from the `obj`
var keys = Object.keys(obj),
    result = {};

// Iterate over all properties in obj
for (var i = 0, len = keys.length; i < len; i++) {
    // If the ID is to be filtered
    if (obj[keys[i]].DepartId === '11') {
        // Add the object in the result object
        result[keys[i]] = obj[keys[i]];
    }
}

var obj = {
    "1": {
        "PerId": "10900662",
        "Name": "Cueball",
        "Email": "cueb@example.com",
        "DepartId": "11"
    },
    "2": {
        "PerId": "10900664",
        "Name": "Megan",
        "MuEmail": "megan@example.com",
        "DepartId": "11"
    },
    "3": {
        "PerId": "10900665",
        "Name": "Beret Guy",
        "MuEmail": "bg@example.com",
        "DepartId": "12"
    }
};
var keys = Object.keys(obj),
    result = {};
for (var i = 0, len = keys.length; i < len; i++) {
    if (obj[keys[i]].DepartId === '11') {
        result[keys[i]] = obj[keys[i]];
    }
}
console.log(result);
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4);


我建议更改数据格式以使用对象数组。然后filter()可以直接应用于数组。

var arr = [{
    "PerId": "10900662",
    "Name": "Cueball",
    "Email": "cueb@example.com",
    "DepartId": "11"
}, {
    "PerId": "10900664",
    "Name": "Megan",
    "MuEmail": "megan@example.com",
    "DepartId": "11"
}, {
    "PerId": "10900665",
    "Name": "Beret Guy",
    "MuEmail": "bg@example.com",
    "DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);

var arr = [{
    "PerId": "10900662",
    "Name": "Cueball",
    "Email": "cueb@example.com",
    "DepartId": "11"
}, {
    "PerId": "10900664",
    "Name": "Megan",
    "MuEmail": "megan@example.com",
    "DepartId": "11"
}, {
    "PerId": "10900665",
    "Name": "Beret Guy",
    "MuEmail": "bg@example.com",
    "DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4);

您正在尝试对对象使用数组过滤器。再次查看该示例。

$([ // this is an array of objects
    {"name":"Lenovo Thinkpad 41A4298","website":"google222"},
    {"name":"Lenovo Thinkpad 41A2222","website":"google"}
  ])
    .filter(function (i,n){
        return n.website==='google';
    });

filter是数组函数的原型,不是object,所以我们不能在这里使用。

为了实现输出,我们可以在object上使用for-in循环

var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"cueb@example.com","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"megan@example.com","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"bg@example.com","DepartId":"12"}}');
var res = {}
for(var key in y){
  if(y[key].DepartId === '11')
   res[key] = y[key]
}
console.log(res)