代码之家  ›  专栏  ›  技术社区  ›  Hello Universe

如何从数组中检索数据,数组中有xml数据的地方?

  •  0
  • Hello Universe  · 技术社区  · 7 年前

    假设我有如下数组

    myArray包含两个值

    0: <maintenanceshortterm type="System.Int32">1111</maintenanceshortterm>
    1: <maintenanceshortterm type="System.Int32">1234</maintenanceshortterm>
    

    我只想使用jquery/js检索值1111和1234。

    请帮助我如何检索这样的值?您可能会猜到数组中的值实际上是通过XML传递的。

    2 回复  |  直到 7 年前
        1
  •  3
  •   CertainPerformance    7 年前

    假设每个项目只是 maintenanceshortterm 而且没有嵌套结构或任何东西,一个简单的正则表达式可以做到这一点:

    const arr = [
    '<maintenanceshortterm type="System.Int32">1111</maintenanceshortterm>',
    '<maintenanceshortterm type="System.Int32">1234</maintenanceshortterm>'
    ];
    console.log(
      arr.map((str) => str.match(/>([^<]+)</)[1])
    );
        2
  •  1
  •   wade king    7 年前

    你也可以使用字符串分割方法

    var arr = new Array('<some xml ...>1111</>', '< some xml...>1234</>');
    
    //split the first string by '>'
    var firstItem = arr[0].split(">");
    //firstItem now contains "<some xml ...", "1111", ""
    //want item at index 1
    document.writeln(firstItem[1]);
    
    //likewise for the second value