我如何遍历一个我不知道它是键的对象?

How can I iterate over an object that I don't know it's keys?

我从服务器得到这样的东西:

{
  "2021-10-13": {
    "1. open": "141.2350",
    "2. high": "141.4000",
    "3. low": "139.2000",
    "4. close": "140.9100",
    "5. volume": "78762721"
  },
  "2021-10-12": {
    "1. open": "143.2300",
    "2. high": "143.2500",
    "3. low": "141.0401",
    "4. close": "141.5100",
    "5. volume": "73035859"
  },
  "2021-10-11": {
    "1. open": "142.2700",
    "2. high": "144.8100",
    "3. low": "141.8100",
    "4. close": "142.8100",
    "5. volume": "64452219"
  }
}

key 值可能因不同的请求而异。如何迭代具有未知键的对象?

比如我想在数据库中写入第二个和第三个嵌套对象(股票),但我不知道应该如何指定它们?

这有帮助吗?

type Keys = {
  "1. open": string;
  "2. high": string;
  "3. low": string;
  "4. close": string;
  "5. volume": string;
}
type B = {[Prop in keyof Keys]: Keys[Prop]}
type A = { [key: string]: B}
const result: A = {
  "2021-10-13": {
    "1. open": "141.2350",
    "2. high": "141.4000",
    "3. low": "139.2000",
    "4. close": "140.9100",
    "5. volume": "78762721"
  },
  "2021-10-12": {
    "1. open": "143.2300",
    "2. high": "143.2500",
    "3. low": "141.0401",
    "4. close": "141.5100",
    "5. volume": "73035859"
  },
  "2021-10-11": {
    "1. open": "142.2700",
    "2. high": "144.8100",
    "3. low": "141.8100",
    "4. close": "142.8100",
    "5. volume": "64452219"
  }
};

这是一种解析您收到的数据并按最新日期排序的方法:

TS Playground

const rawSummaryKeys = ['1. open', '2. high', '3. low', '4. close', '5. volume'] as const;
type RawSummaryKey = typeof rawSummaryKeys[number];

const parsedSummaryKeys = ['open', 'high', 'low', 'close', 'volume'] as const;
type ParsedSummaryKey = typeof parsedSummaryKeys[number];

const rawToParsedSummaryKeyMapping: Record<RawSummaryKey, ParsedSummaryKey> = {
  '1. open': 'open',
  '2. high': 'high',
  '3. low': 'low',
  '4. close': 'close',
  '5. volume': 'volume',
};

/** Values are parsable as numbers */
type RawSummary = Record<RawSummaryKey, string>;

/** Keys are dates in format: YYYY-MM-DD */
type DailyRawSummaries = Record<string, RawSummary>;

type ParsedSummary = Record<ParsedSummaryKey, number>;

function parseRawSummary (summary: RawSummary): ParsedSummary {
  const parsed = {} as ParsedSummary;
  for (const key of rawSummaryKeys) {
    // If the "volume" number ever exceeds Number.MAX_SAFE_INTEGER,
    // then you can switch to using BigInts
    parsed[rawToParsedSummaryKeyMapping[key]] = Number(summary[key]);
  }
  return parsed;
}

type DailySummaryEntrry = [date: string, summary: ParsedSummary];

function parseDailySummaries (summaries: DailyRawSummaries): DailySummaryEntrry[] {
  const entries: DailySummaryEntrry[] = [];

  for (const date in summaries) {
    const rawSummary = summaries[date];
    if (!rawSummary) continue;
    entries.push([date, parseRawSummary(rawSummary)]);
  }

  return entries.sort().reverse(); // sort by newest date first
}

function main () {
  const json = `{"2021-10-13":{"1. open":"141.2350","2. high":"141.4000","3. low":"139.2000","4. close":"140.9100","5. volume":"78762721"},"2021-10-12":{"1. open":"143.2300","2. high":"143.2500","3. low":"141.0401","4. close":"141.5100","5. volume":"73035859"},"2021-10-11":{"1. open":"142.2700","2. high":"144.8100","3. low":"141.8100","4. close":"142.8100","5. volume":"64452219"}}`;
  const raw: DailyRawSummaries = JSON.parse(json);

  const parsed = parseDailySummaries(raw);

  for (const [date, summary] of parsed) {
    console.log(date, summary);
  }
}

main();

从 TS 游乐场转译的 JS:

const rawSummaryKeys = ['1. open', '2. high', '3. low', '4. close', '5. volume'];
const parsedSummaryKeys = ['open', 'high', 'low', 'close', 'volume'];
const rawToParsedSummaryKeyMapping = {
    '1. open': 'open',
    '2. high': 'high',
    '3. low': 'low',
    '4. close': 'close',
    '5. volume': 'volume',
};
function parseRawSummary(summary) {
    const parsed = {};
    for (const key of rawSummaryKeys) {
        // If the "volume" number ever exceeds Number.MAX_SAFE_INTEGER,
        // then you can switch to using BigInts
        parsed[rawToParsedSummaryKeyMapping[key]] = Number(summary[key]);
    }
    return parsed;
}
function parseDailySummaries(summaries) {
    const entries = [];
    for (const date in summaries) {
        const rawSummary = summaries[date];
        if (!rawSummary)
            continue;
        entries.push([date, parseRawSummary(rawSummary)]);
    }
    return entries.sort().reverse(); // sort by newest date first
}
function main() {
    const json = `{"2021-10-13":{"1. open":"141.2350","2. high":"141.4000","3. low":"139.2000","4. close":"140.9100","5. volume":"78762721"},"2021-10-12":{"1. open":"143.2300","2. high":"143.2500","3. low":"141.0401","4. close":"141.5100","5. volume":"73035859"},"2021-10-11":{"1. open":"142.2700","2. high":"144.8100","3. low":"141.8100","4. close":"142.8100","5. volume":"64452219"}}`;
    const raw = JSON.parse(json);
    const parsed = parseDailySummaries(raw);
    for (const [date, summary] of parsed) {
        console.log(date, summary);
    }
}
main();