Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算多维数组中的项目总数?_Javascript_Node.js_Selenium Webdriver_Protractor_Selenium Chromedriver - Fatal编程技术网

Javascript 如何计算多维数组中的项目总数?

Javascript 如何计算多维数组中的项目总数?,javascript,node.js,selenium-webdriver,protractor,selenium-chromedriver,Javascript,Node.js,Selenium Webdriver,Protractor,Selenium Chromedriver,下面是我用来从repeater表中检索数据的代码。 这将以多维数组形式提供输出。请检查输出 我想得到整个多维数组中的项目总数和每个数组中的项目总数 更详细地说,整个多维数组有30个项,每个数组中有6个项,我想得到这两个值。 如何计算这两个值 var RepeaterTable = element.all(by.repeater("view in ctrl.view track by $index")).each(function(rowelem,index){

下面是我用来从repeater表中检索数据的代码。 这将以多维数组形式提供输出。请检查输出

我想得到整个多维数组中的项目总数和每个数组中的项目总数

更详细地说,整个多维数组有30个项,每个数组中有6个项,我想得到这两个值。 如何计算这两个值

var RepeaterTable = element.all(by.repeater("view in ctrl.view track by $index")).each(function(rowelem,index){
                     rowelem.getText().then(function(BlockTrans){
                         console.log("****index and RowElem\n"+index+"\n",BlockTrans);
                         var item = BlockTrans;
                     });


                 });  
上述代码的输出
递归地遍历每个元素,如果是数组,则计算所有元素并返回数量

示例

function getTotalElementCount (obj) {
    /* This number will contain the total amount of elements in the given variable obj.
     * This means all subelements will also be counted there.
     */
    let count = 0; 
    // If obj is an array, we want to find out how many elements and subelements it contains
    if (obj instanceof Array) {
        // We call this function for every element (elem) in obj to get the total amount of elements in elem
        obj.foreach ((elem) => {
            // We increment the count by all elements in elem
            count += getTotalElementCount(elem);
        }
    } else {
         /* Element is not an array, so we can't go deeper. 
          * This means this obj is only a single element
          */
        return 1;
        /* you can also do this.
         * count = 1;
         */
    }
    return count;
}

请告诉我,如果我的例子不够清楚,您是否在量角器中尝试了计数方法

例如:

countElement(locator: string) {
    return element.all(by.css(locator)).count();
}

如何计算这两个值?。整个数组中的总数和每个单个数组中的总数。代码不太清楚。如果
obj
不是数组,为什么要返回1?请提供更多帮助。我添加了一些注释。请告诉我,如果有什么是关闭的有31个值,而不是2。您将有30行数字和1个总数。
countElement(locator: string) {
    return element.all(by.css(locator)).count();
}