首页 > 解决方案 > 如何使用Javascript从数组中获取选定的字段?

问题描述

请多多包涵,我是编码新手。我正在尝试测试 Web 服务,但我遇到了一个 java 脚本数组,我需要从 10 个字段中获取 2 个字段以进行进一步测试。如果有人可以帮助我,我将不胜感激?

标签: javascriptarraystestcomplete

解决方案


尝试这个!

<script>
        let colors = ["Red", "Green", "Blue"];
        alert(colors[2]);
</script>

或更通用:

<script>
    let colors = ["Red", "Green", "Blue"];

    //Output: Blue (Note that arrays starts with 0)
    PrintArrayValue(colors, 2);

    function PrintArrayValue(array, index){
        alert(array[index]);
    }
</script>

推荐阅读