javascript - 在嵌套数组中获取不同的键
问题描述
我有一个数组,我想在上面获取所有不同的键。我的 json 是这样的:
{
"odds": {
"1_2": [
{
"id": 3355,
"handicap": "1.25",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 3361,
"handicap": "1.5",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 559332,
"handicap": "+1.0,+1.5",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 684091,
"handicap": "+0.5,+1.0",
"home_od": {
"odds": "2.150",
"status": "same"
},
"away_od": {
"odds": "1.675",
"status": "same"
}
},
{
"id": 687351,
"handicap": "+1",
"home_od": {
"odds": "1.825",
"status": "same"
},
"away_od": {
"odds": "1.975",
"status": "same"
}
},
{
"id": 838672,
"handicap": "+0.5",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 891523,
"handicap": "0.0,+0.5",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 990467,
"handicap": "-0.5",
"home_od": {
"odds": "1.850",
"status": "same"
},
"away_od": {
"odds": "1.950",
"status": "same"
}
},
{
"id": 1154786,
"handicap": "-0.5,-1.0",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 1265769,
"handicap": "0.0",
"home_od": {
"odds": "2.300",
"status": "same"
},
"away_od": {
"odds": "1.600",
"status": "same"
}
}
],
"1_3": [
{
"id": 3371,
"handicap": "3.25",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 3380,
"handicap": "3.5",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 559335,
"handicap": "3.0,3.5",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 691481,
"handicap": "3",
"over_od": {
"odds": "1.800",
"status": "same"
},
"under_od": {
"odds": "2.000",
"status": "same"
}
},
{
"id": 742454,
"handicap": "2.5,3.0",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 789160,
"handicap": "2.5",
"over_od": {
"odds": "1.800",
"status": "same"
},
"under_od": {
"odds": "2.000",
"status": "same"
}
},
{
"id": 835770,
"handicap": "2.0,2.5",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 882149,
"handicap": "2",
"over_od": {
"odds": "1.750",
"status": "same"
},
"under_od": {
"odds": "2.050",
"status": "same"
}
},
{
"id": 916746,
"handicap": "1.5,2.0",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 1232302,
"handicap": "4",
"over_od": {
"odds": "1.875",
"status": "same"
},
"under_od": {
"odds": "1.925",
"status": "same"
}
},
{
"id": 1253826,
"handicap": "3.5,4.0",
"over_od": {
"odds": "1.700",
"status": "same"
},
"under_od": {
"odds": "2.100",
"status": "same"
}
},
{
"id": 1355508,
"handicap": "4.5",
"over_od": {
"odds": "3.450",
"status": "same"
},
"under_od": {
"odds": "1.300",
"status": "same"
}
}
]
}}
我试图通过这种方式得到它们
for (var i = 0; i < titles.length; i++) {
var keys = [];
for (var key in [titles[i]]) {
keys.push(key);
}
console.log(odds[titles[i]][keys[0]]);
我可以使用这个代码odds[titles[i]][j].id访问每个ojbect的id
如何访问这些不同的键并在我的代码中显示它们?例如,我想显示 home_od 名称并显示其中包含的赔率和状态
解决方案
检查以下工作代码以转换为所需的对象数组以在 UI 中显示。
每个对象将拥有的位置
{
"key": "1_2",
"id": 3355,
"name-home_od": "home_od",
"odds-home_od": "2.000",
"name-away_od": "away_od",
"odds-away_od": "1.800"
}
name-home_od
将其拆分,-
您将home-od
在 UI 中进行更新。
odds-home_od
将其拆分,-
您将home-od.odds
在 UI 中进行更新。
希望这可以帮助。(一行代码;)
const input = {
"odds": {
"1_2": [
{
"id": 3355,
"handicap": "1.25",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 3361,
"handicap": "1.5",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 559332,
"handicap": "+1.0,+1.5",
"home_od": {
"odds": "2.000",
"status": "same"
},
"away_od": {
"odds": "1.800",
"status": "same"
}
},
{
"id": 684091,
"handicap": "+0.5,+1.0",
"home_od": {
"odds": "2.150",
"status": "same"
},
"away_od": {
"odds": "1.675",
"status": "same"
}
},
{
"id": 687351,
"handicap": "+1",
"home_od": {
"odds": "1.825",
"status": "same"
},
"away_od": {
"odds": "1.975",
"status": "same"
}
},
{
"id": 838672,
"handicap": "+0.5",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 891523,
"handicap": "0.0,+0.5",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 990467,
"handicap": "-0.5",
"home_od": {
"odds": "1.850",
"status": "same"
},
"away_od": {
"odds": "1.950",
"status": "same"
}
},
{
"id": 1154786,
"handicap": "-0.5,-1.0",
"home_od": {
"odds": "2.025",
"status": "same"
},
"away_od": {
"odds": "1.775",
"status": "same"
}
},
{
"id": 1265769,
"handicap": "0.0",
"home_od": {
"odds": "2.300",
"status": "same"
},
"away_od": {
"odds": "1.600",
"status": "same"
}
}
],
"1_3": [
{
"id": 3371,
"handicap": "3.25",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 3380,
"handicap": "3.5",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 559335,
"handicap": "3.0,3.5",
"over_od": {
"odds": "1.900",
"status": "same"
},
"under_od": {
"odds": "1.900",
"status": "same"
}
},
{
"id": 691481,
"handicap": "3",
"over_od": {
"odds": "1.800",
"status": "same"
},
"under_od": {
"odds": "2.000",
"status": "same"
}
},
{
"id": 742454,
"handicap": "2.5,3.0",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 789160,
"handicap": "2.5",
"over_od": {
"odds": "1.800",
"status": "same"
},
"under_od": {
"odds": "2.000",
"status": "same"
}
},
{
"id": 835770,
"handicap": "2.0,2.5",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 882149,
"handicap": "2",
"over_od": {
"odds": "1.750",
"status": "same"
},
"under_od": {
"odds": "2.050",
"status": "same"
}
},
{
"id": 916746,
"handicap": "1.5,2.0",
"over_od": {
"odds": "1.775",
"status": "same"
},
"under_od": {
"odds": "2.025",
"status": "same"
}
},
{
"id": 1232302,
"handicap": "4",
"over_od": {
"odds": "1.875",
"status": "same"
},
"under_od": {
"odds": "1.925",
"status": "same"
}
},
{
"id": 1253826,
"handicap": "3.5,4.0",
"over_od": {
"odds": "1.700",
"status": "same"
},
"under_od": {
"odds": "2.100",
"status": "same"
}
},
{
"id": 1355508,
"handicap": "4.5",
"over_od": {
"odds": "3.450",
"status": "same"
},
"under_od": {
"odds": "1.300",
"status": "same"
}
}
]
}}
const flat_objs = Object.keys(input.odds)
.reduce(
(acc, curr) => [
...acc,
...input.odds[curr].map(item => ({ ...item, key: curr }))
],
[]
)
.map(obj => ({
key: obj.key,
id: obj.id,
...Object.keys(obj)
.filter(f => f.includes("_"))
.reduce(
(acc, curr) => ({
...acc,
[`name-${curr}`]: curr,
[`odds-${curr}`]: obj[curr].odds
}),
{}
)
}));
console.log(flat_objs)
for (let obj of flat_objs) {
Object.keys(obj)
.filter(f => f.includes("name-"))
.map(key =>
console.log(
"name: ",
key.split("-")[1],
", odds: ",
obj[`odds-${key.split("-")[1]}`]
)
);
}
提取名称和赔率的示例。
for (let obj of flat_objs) {
Object.keys(obj)
.filter(f => f.includes("name-"))
.map(key =>
console.log(
"name: ",
key.split("-")[1],
", odds: ",
obj[`odds-${key.split("-")[1]}`]
)
);
}
推荐阅读
- python - 如何从 openstack shade 转移到 openstacksdk?
- kubernetes - Keycloak 网守 HTTP 502 / 10 秒后超时
- html - 用条纹分隔标题和其余部分
- swift - 如何将整数“1”的字符快速转换为字符串“One”
- python - 在 MacOS 中调试 Numba
- eclipse-rcp - Eclipse RCP 插件代码覆盖与 Jacoco 和 Tycho 万无一失
- python - Python调用字符串的变量名一个int
- javascript - React js:会话过期后如何自动注销?
- c# - c# Thread.Equals vs Thread.CurrentThread.ManagedThreadId 比较
- javascript - Highcharts 渲染周期(无数标签格式化程序调用)