gpt4 book ai didi

javascript - 限制来自 JSON 文件的下拉菜单

转载 作者:行者123 更新时间:2023-11-30 14:47:43 24 4
gpt4 key购买 nike

我想将我的 JSON 文件解析为一个选择框,但我希望下拉列表中只有一个值,而不是相同 KEY 的重复值。

{
"car_make": "CADILLAC",
"model": "CTS SPORTS WAGON MANUAL V8 RWD SUPERCHARGED",
"engine_capacity": 6200.0,
"body_type": "S/WAGON",
"crsp": 5280463.0
},
{
"car_make": "CADILLAC",
"model": "CTS V WAGON MANUAL V8 RWD SUPERCHARGED",
"engine_capacity": 6200.0,
"body_type": "S/WAGON",
"crsp": 8767560.0
},
{
"car_make": "CADILLAC",
"model": "STS MANUAL V8 AWD",
"engine_capacity": 3600.0,
"body_type": "SAL",
"crsp": 6507501.0
},
{
"car_make": "CADILLAC",
"model": "DTS AUTO V8",
"engine_capacity": 4600.0,
"body_type": "SAL",
"crsp": 6424919.0
},
{
"car_make": "CADILLAC",
"model": "SRX CROSSOVER FWD AUTO MANUAL V6",
"engine_capacity": 3000.0,
"body_type": "SUV",
"crsp": 4764322.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE V8 AWD AUTO",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 8733788.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE HYBRID AUTO 2WD SFI RWD HYBRID",
"engine_capacity": 6000.0,
"body_type": "SUV",
"crsp": 10203756.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE ESV AUTO V8 AWD",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 9095086.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE EXT AUTO V8 AWD",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 8555347.0
}

在这种情况下,我只希望 car_make 成为下拉列表中的一个,这样 cadillac 就不会出现多次,希望你明白我想问的问题。我尝试使用 Javascript 和 flask 。

最佳答案

您可以混合使用 Array#mapArray#filter获得独特的 car_make:

let data = [{
"car_make": "CADILLAC",
"model": "CTS SPORTS WAGON MANUAL V8 RWD SUPERCHARGED",
"engine_capacity": 6200.0,
"body_type": "S/WAGON",
"crsp": 5280463.0
},
{
"car_make": "CADILLAC",
"model": "CTS V WAGON MANUAL V8 RWD SUPERCHARGED",
"engine_capacity": 6200.0,
"body_type": "S/WAGON",
"crsp": 8767560.0
},
{
"car_make": "CADILLAC",
"model": "STS MANUAL V8 AWD",
"engine_capacity": 3600.0,
"body_type": "SAL",
"crsp": 6507501.0
},
{
"car_make": "CADILLAC",
"model": "DTS AUTO V8",
"engine_capacity": 4600.0,
"body_type": "SAL",
"crsp": 6424919.0
},
{
"car_make": "CADILLAC",
"model": "SRX CROSSOVER FWD AUTO MANUAL V6",
"engine_capacity": 3000.0,
"body_type": "SUV",
"crsp": 4764322.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE V8 AWD AUTO",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 8733788.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE HYBRID AUTO 2WD SFI RWD HYBRID",
"engine_capacity": 6000.0,
"body_type": "SUV",
"crsp": 10203756.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE ESV AUTO V8 AWD",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 9095086.0
},
{
"car_make": "CADILLAC",
"model": "ESCALADE EXT AUTO V8 AWD",
"engine_capacity": 6200.0,
"body_type": "SUV",
"crsp": 8555347.0
}
];

let result = data
.map(item => item.car_make)
.filter((item, index, array) => {
return array.indexOf(item) === index;
});

console.log(result);

关于javascript - 限制来自 JSON 文件的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48644662/

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