带键值的数组长度,js [英] array length with key-value, js

查看:71
本文介绍了带键值的数组长度,js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码,应该解析给定的paramUnparsed(应该是以下形式的数组: [key1 = val1,key2 = val2,..,keyn = valn] ).

I wrote this piece of code, that should parse the given paramUnparsed (which should be an array in the form: [key1=val1, key2=val2, .., keyn=valn]).

function parseParams(paramUnparsed){
var params = [];
for ( var j = 0; j < paramUnparsed.length; j++) {
    if (paramUnparsed[j].split('=').length < 2) {
        // error ! bad input structure, ignoring params -

        params = undefined;
        break; // we don't have to return error, depending
                // on the function called and given params.

    }
    //else {
        var key = paramUnparsed[j].split('=')[0];
        var value = paramUnparsed[j].split('=')[1];
        params[key] = value;
    //}
}

console.log("In parseParams, params are: "+ concatObject(params));//DEBUG 1
console.log("In parseParams, params length is: "+ params.length);//DEBUG 2
return params;  
}

我该怎么做,仍然确定我要创建的数组的长度?我总是在'DEBUG 2'打印输出上得到'0'...

How can I do this, and still determine the length of the array that I'm creating? I always get '0' on the 'DEBUG 2' printout...

推荐答案

创建的"params"数组是一个关联数组.如果要获取关联数组的长度,请使用

The "params" array which is created is an associative array. If you want to get the length of the associative array use

Object.keys(params).length;

这篇关于带键值的数组长度,js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆