8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

为 JavaScript 字典分配动态值

BigBen 2月前

112 0

我需要将动态值分配给 JavaScript 字典以用于 echarts,但是在将动态值分配给字典时,它显示的行为与分配 s 相比有所不同......

我需要将动态值分配给 JavaScript 字典以用于 echarts,但是当将动态值分配给字典时,它显示的行为与分配静态值不同。

分配静态值时:

     response = {
            "stacks": {"5G_L2": [{"Open": "43"},{"Close": "24"}],
                       "5G_L3": [{"Open": "12"},{"Close": "2"}]
                      }
                  }; 

调试窗口显示如下:

enter image description here

而当动态分配值时,如下所示:

            var datastck=[];
            var serverdata = '{{ barCdata | tojson }}';
            resPbar = $.parseJSON(serverdata);

            $.each(resPbar, function (i, item) {
              var di={};
              di[item.Team]=[{"Open": item.Open},{"Close": item.Close}];
              datastck.push(di);
            });

            
          response = {
            "stacks": datastck
                  };

调试窗口如下:

enter image description here

它添加了一个额外的数组层。我需要有静态分配结构来支持 echarts,请有人提供建议?

提前致谢。

帖子版权声明 1、本帖标题:为 JavaScript 字典分配动态值
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由BigBen在本站《dictionary》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 我正在尝试对 react-native 项目进行一些测试,但是 jest 遇到了这个错误,我不知道为什么:FAIL __tests__/(tabs)/weather.test.tsx 测试套件无法运行 Jest

    我正在尝试对 react-native 项目进行一些测试,但是 jest 遇到了这个错误,我不知道原因:

     FAIL  __tests__/(tabs)/weather.test.tsx
       Test suite failed to run
    
        Jest encountered an unexpected token
    
        Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    
        Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    
        By default "node_modules" folder is ignored by transformers.
    
        Here's what you can do:
         • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
         • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
         • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
         • If you need a custom transformation specify a "transform" option in your config.
         • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    
        You'll find more details and examples of these config options in the docs:
        https://jestjs.io/docs/configuration
        For information about custom transformations, see:
        https://jestjs.io/docs/code-transformation
    
        Details:
    
        /Users/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
        import * as React2 from "react";
        ^^^^^^
    
        SyntaxError: Cannot use import statement outside a module
    
          3 | import { render } from "@testing-library/react-native";
          4 | import React, { PropsWithChildren } from "react";
        > 5 | import { Provider } from "react-redux";
            | ^
          6 |
          7 | const AllTheProviders = ({ children }: PropsWithChildren) => {
          8 |   return (
    
          at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
          at Object.require (test-utils.tsx:5:1)
          at Object.require (/Users/j/Downloads/tdd-expo/__tests__/(tabs)../../../../weather.test.tsx:2:1)
    

    这似乎是失败的地方:

        Details:
    
        /Users/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
        import * as React2 from "react";
        ^^^^^^
    

    因此,无法将其导入文件 test-utils.tsx

    // test-utils.tsx
    import { DarkTheme, ThemeProvider } from "@react-navigation/native";
    import "@testing-library/jest-native/extend-expect";
    import { render } from "@testing-library/react-native";
    import React, { PropsWithChildren } from "react";
    import { Provider } from "react-redux"; // Is failing to import here
    
    const AllTheProviders = ({ children }: PropsWithChildren) => {
      return (
        <Provider store={{} as any}>
          <ThemeProvider value={DarkTheme}>{children}</ThemeProvider>
        </Provider>
      );
    };
    
    const customRender = (ui: React.ReactElement, options: any) =>
      render(ui, { wrapper: AllTheProviders, ...options });
    
    // re-export everything
    export * from "@testing-library/react-native";
    
    // override render method
    export { customRender as render };
    

    以下是 jest 的配置:

    // package.json
    {
    // ...
      "jest": {
        "preset": "jest-expo",
        "transformIgnorePatterns": [
          "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
        ],
        "collectCoverage": true
      },
    
    // ...
    }
    

    我遵循了以下指南: https://docs.expo.dev/develop/unit-testing/

    并尝试了这个自定义渲染设置: https://testing-library.com/docs/react-testing-library/setup/#custom-render

    我期望 jest 能够成功运行,但实际上它导致了“import”错误。

    知道发生了什么吗?提前致谢

返回
作者最近主题: