使用 .filter() 过滤 javascript 中的对象数组

filtering an array of objects in javascript using .filter()

我想过滤 javascript 中的对象数组,最好使用使用扇区值的过滤方法

示例输入

[
    {
        "columns": {
            "ccy": {
                "style": 7,
                "value": "EUR",
                "formatted_value": "EUR",
                "error": null
            },
            "country": {
                "style": 7,
                "value": "France",
                "formatted_value": "France",
                "error": null
            },
            "sector": {
                "style": 7,
                "value": "Utilities",
                "formatted_value": "Utilities",
                "error": null
            },
            "isin": {
                "style": 7,
                "value": "FR0011374099",
                "formatted_value": "FR0011374099",
                "error": null
            },
            "instrument": {
                "style": 7,
                "value": "AF 6.25 18Jan18",
                "formatted_value": "AF 6.25 18Jan18",
                "error": null
            },
            "price": {
                "style": 8,
                "value": 1.08,
                "formatted_value": "1.08",
                "error": null
            },
            "bid": {
                "style": 9,
                "value": 1,
                "formatted_value": "1.000",
                "error": null
            },
            "ask": {
                "style": 9,
                "value": 1.15,
                "formatted_value": "1.150",
                "error": null
            },
            "coupon": {
                "style": 10,
                "value": 0.01,
                "formatted_value": "1.00%",
                "error": null
            },
            "date": {
                "style": 11,
                "value": 43118,
                "formatted_value": "18-Jan-2018 00:00:00",
                "error": null
            },
            "rating": {
                "style": 5,
                "value": "C",
                "formatted_value": "C",
                "error": null
            },
            "position": {
                "style": 7,
                "value": null,
                "formatted_value": null,
                "error": null
            },
            "nav": {
                "style": 12,
                "value": null,
                "formatted_value": null,
                "error": null
            }
        },
        "index": 4
    },
    {
        "columns": {
            "ccy": {
                "style": 0,
                "value": "EUR",
                "formatted_value": "EUR",
                "error": null
            },
            "country": {
                "style": 0,
                "value": "France",
                "formatted_value": "France",
                "error": null
            },
            "sector": {
                "style": 0,
                "value": "Transport",
                "formatted_value": "Transport",
                "error": null
            },
            "isin": {
                "style": 0,
                "value": "FR0010804500",
                "formatted_value": "FR0010804500",
                "error": null
            },
            "instrument": {
                "style": 0,
                "value": "AREVAG 4.875 23Sep24",
                "formatted_value": "AREVAG 4.875 23Sep24",
                "error": null
            },
            "price": {
                "style": 1,
                "value": 0.86,
                "formatted_value": "0.86",
                "error": null
            },
            "bid": {
                "style": 2,
                "value": 0.79,
                "formatted_value": "0.790",
                "error": null
            },
            "ask": {
                "style": 2,
                "value": 0.94,
                "formatted_value": "0.940",
                "error": null
            },
            "coupon": {
                "style": 3,
                "value": 0.04875,
                "formatted_value": "4.88%",
                "error": null
            },
            "date": {
                "style": 4,
                "value": 44203.5243055556,
                "formatted_value": "07-Jan-2021 12:35:00",
                "error": null
            },
            "rating": {
                "style": 5,
                "value": "NR",
                "formatted_value": "NR",
                "error": null
            },
            "position": {
                "style": 0,
                "value": null,
                "formatted_value": null,
                "error": null
            },
            "nav": {
                "style": 6,
                "value": null,
                "formatted_value": null,
                "error": null
            }
        },
        "index": 5
    },
    {
        "columns": {
            "ccy": {
                "style": 7,
                "value": "EUR",
                "formatted_value": "EUR",
                "error": null
            },
            "country": {
                "style": 7,
                "value": "France",
                "formatted_value": "France",
                "error": null
            },
            "sector": {
                "style": 7,
                "value": "Transport",
                "formatted_value": "Transport",
                "error": null
            },
            "isin": {
                "style": 7,
                "value": "FR0011791391",
                "formatted_value": "FR0011791391",
                "error": null
            },
            "instrument": {
                "style": 7,
                "value": "AREVAG 3.125 20Mar23",
                "formatted_value": "AREVAG 3.125 20Mar23",
                "error": null
            },
            "price": {
                "style": 8,
                "value": 0.83,
                "formatted_value": "0.83",
                "error": null
            },
            "bid": {
                "style": 9,
                "value": 1.333,
                "formatted_value": "1.333",
                "error": null
            },
            "ask": {
                "style": 9,
                "value": 0.9,
                "formatted_value": "0.900",
                "error": null
            },
            "coupon": {
                "style": 10,
                "value": 0.001,
                "formatted_value": "0.10%",
                "error": null
            },
            "date": {
                "style": 11,
                "value": 45731,
                "formatted_value": "15-Mar-2025 00:00:00",
                "error": null
            },
            "rating": {
                "style": 5,
                "value": "BB",
                "formatted_value": "BB",
                "error": null
            },
            "position": {
                "style": 7,
                "value": null,
                "formatted_value": null,
                "error": null
            },
            "nav": {
                "style": 12,
                "value": null,
                "formatted_value": null,
                "error": null
            }
        },
        "index": 6
    }
]

预期产出:(公用事业部门)

[
    {
        "columns": {
            "ccy": {
                "style": 7,
                "value": "EUR",
                "formatted_value": "EUR",
                "error": null
            },
            "country": {
                "style": 7,
                "value": "France",
                "formatted_value": "France",
                "error": null
            },
            "sector": {
                "style": 7,
                "value": "Utilities",
                "formatted_value": "Utilities",
                "error": null
            },
            "isin": {
                "style": 7,
                "value": "FR0011374099",
                "formatted_value": "FR0011374099",
                "error": null
            },
            "instrument": {
                "style": 7,
                "value": "AF 6.25 18Jan18",
                "formatted_value": "AF 6.25 18Jan18",
                "error": null
            },
            "price": {
                "style": 8,
                "value": 1.08,
                "formatted_value": "1.08",
                "error": null
            },
            "bid": {
                "style": 9,
                "value": 1,
                "formatted_value": "1.000",
                "error": null
            },
            "ask": {
                "style": 9,
                "value": 1.15,
                "formatted_value": "1.150",
                "error": null
            },
            "coupon": {
                "style": 10,
                "value": 0.01,
                "formatted_value": "1.00%",
                "error": null
            },
            "date": {
                "style": 11,
                "value": 43118,
                "formatted_value": "18-Jan-2018 00:00:00",
                "error": null
            },
            "rating": {
                "style": 5,
                "value": "C",
                "formatted_value": "C",
                "error": null
            },
            "position": {
                "style": 7,
                "value": null,
                "formatted_value": null,
                "error": null
            },
            "nav": {
                "style": 12,
                "value": null,
                "formatted_value": null,
                "error": null
            }
        },
        "index": 4
    }
  }
]

我尝试过的:

const myFilteredData = myUnfilteredData.filter((data) => data.sector.value === 'Utilities');

我的困惑来自于我正在处理的数据的结构,它是一个像这样的对象数组。

我需要先 .map 然后 .filter 在那个地图的 return 上?

您能做的最好的事情就是为您想要的结果提供新变量并迭代数据数组。

例如:

const result = [];

data.forEach(e => {
    if (e.columns.sector.value && e.columns.sector.value === 'Sales') {
        /**
            you can push what you want to map. for example
            result.push({
               columns: e.columns,
               sector: e.columns.sector.value
            })
        **/
        result.push(e);
    }
});

您可以将您想要从原始数据中提取的任何对象推送到新变量。这样你只迭代你的数据数组一次。

sector 在里面 columns:

const myFilteredData = myUnfilteredData.filter(data => 
  data.columns.sector.value === 'Utilities'
);