代码之家  ›  专栏  ›  技术社区  ›  WDR

我如何迭代一个我不知道它是键的对象?

  •  0
  • WDR  · 技术社区  · 2 年前

    我从服务器上得到这样的信息:

    {
      "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 根据不同的请求,值可能会有所不同。如何迭代具有未知键的对象?

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

    0 回复  |  直到 2 年前
        1
  •  1
  •   jsejcksn    2 年前

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

    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();
        2
  •  1
  •   beautifulcoder    2 年前

    这有用吗?

    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"
      }
    };