我有一个这样的javascript对象:
var obj = {
HEALTH: {
business: "HEALTH",
percentage: 0,
outerCriteria:{
DEFAULT:[
{
thePercentage : 10,
innerCriteria : {
min: '10',
max: '20',
city: 'Denver',
}
}
],
RANDOM:[
{
thePercentage : 20,
innerCriteria : {
min: '50',
max: '20',
city: 'Darlington',
}
},
{
thePercentage : 30,
innerCriteria : {
min: '50',
max: '60',
city: 'Barrington',
}
},
{
thePercentage : 36,
innerCriteria : {
min: '20',
max: '62',
city: 'Barrington',
}
},
],
NEW:[
{
thePercentage : 5,
innerCriteria : {
min: '5',
max: '20',
city: 'Darlington',
}
},
{
thePercentage : 6,
innerCriteria : {
min: '5',
max: '60',
city: 'Darlington',
}
},
{
thePercentage : 36,
innerCriteria : {
min: '20',
max: '62',
city: 'Barrington',
}
},
],
}
}
}
我希望根据 city 对此进行过滤。例如,如果我过滤了 Darlington ,则所需的输出将是:
var obj = {
HEALTH: {
business: "HEALTH",
percentage: 0,
outerCriteria:{
RANDOM:[
{
thePercentage : 20,
innerCriteria : {
min: '50',
max: '20',
city: 'Darlington',
}
},
],
NEW:[
{
thePercentage : 5,
innerCriteria : {
min: '5',
max: '20',
city: 'Darlington',
}
},
{
thePercentage : 6,
innerCriteria : {
min: '5',
max: '60',
city: 'Darlington',
}
},
],
}
}
}
我尝试使用过滤器和地图,但没有用。我尝试过滤一些,但无法通过。
堆栈溢出不允许我发布此问题,以提供更多详细信息。我没有更多细节了。这就是我所拥有的。如果有人要问,您可以随时发表评论。
请帮助。 谢谢。
答案 0 :(得分:0)
您可以使用array.filter()
方法按城市进行过滤,以及使用delete()
方法来删除对象属性(如果与给定键关联的数组为空)。
示例功能:
function filterByCity(city) {
for(let o in obj.HEALTH.outerCriteria) {
obj.HEALTH.outerCriteria[o] = obj.HEALTH.outerCriteria[o].filter((item) => item.innerCriteria.city == city);
if(obj.HEALTH.outerCriteria[o].length == 0) { // if the array is empty delete it
delete(obj.HEALTH.outerCriteria[o]);
}
}
}
运行并测试:
var obj = {
HEALTH: {
business: "HEALTH",
percentage: 0,
outerCriteria:{
DEFAULT:[
{
thePercentage : 10,
innerCriteria : {
min: '10',
max: '20',
city: 'Denver',
}
}
],
RANDOM:[
{
thePercentage : 20,
innerCriteria : {
min: '50',
max: '20',
city: 'Darlington',
}
},
{
thePercentage : 30,
innerCriteria : {
min: '50',
max: '60',
city: 'Barrington',
}
},
{
thePercentage : 36,
innerCriteria : {
min: '20',
max: '62',
city: 'Barrington',
}
},
],
NEW:[
{
thePercentage : 5,
innerCriteria : {
min: '5',
max: '20',
city: 'Darlington',
}
},
{
thePercentage : 6,
innerCriteria : {
min: '5',
max: '60',
city: 'Darlington',
}
},
{
thePercentage : 36,
innerCriteria : {
min: '20',
max: '62',
city: 'Barrington',
}
},
],
}
}
}
function filterByCity(city) {
for(let o in obj.HEALTH.outerCriteria) {
obj.HEALTH.outerCriteria[o] = obj.HEALTH.outerCriteria[o].filter((item) => item.innerCriteria.city == city);
if(obj.HEALTH.outerCriteria[o].length == 0) { // if the array is empty delete it
delete(obj.HEALTH.outerCriteria[o]);
}
}
}
filterByCity('Darlington');
console.log(obj);
答案 1 :(得分:-1)
var unfilteredOuterCriteria = obj.HEALTH.outerCriteria;
var filteredOuterCriteria = {};
for(attr in obj.HEALTH.outerCriteria){
filteredOuterCriteria[attr] =
obj.HEALTH.outerCriteria[attr].filter(criteria => criteria.innerCriteria.city === 'Darlington');
}
obj.HEALTH.outerCriteria = filteredOuterCriteria;