gpt4 book ai didi

javascript - 过滤具有特定值的数组

转载 作者:行者123 更新时间:2023-11-30 00:10:04 24 4
gpt4 key购买 nike

我有这个 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 进行过滤

这是我测试过的代码,来自this Stack Overflow question :

<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 不是函数。

我希望 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 原型(prototype)上定义。它不能用于对象。

您可以使用 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);

关于javascript - 过滤具有特定值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com