admin管理员组文章数量:1429024
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;
}
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...
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;
}
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...
Share Improve this question asked Dec 16, 2011 at 10:42 limlimlimlim 3,1552 gold badges37 silver badges46 bronze badges 04 Answers
Reset to default 12The "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;
You are creating an array, but you are not using it as an array. You are using it as an object, which works as an array is also an object, but it means that the length of the array remains zero as you are not adding any array items.
Create an object, and count the items that you add to get the count:
function parseParams(paramUnparsed){
var params = {}, cnt = 0;
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.
}
var key = paramUnparsed[j].split('=')[0];
var value = paramUnparsed[j].split('=')[1];
params[key] = value;
cnt++;
}
console.log("In parseParams, params are: "+ concatObject(params));//DEBUG 1
console.log("In parseParams, params length is: "+ cnt);//DEBUG 2
return params;
}
You're not really building an array. Arrays shouldn't have keys. You can use an object like this:
var params = {};
but that wouldn't solve your .length
issue.
What you can also do is building an array of objects, where each object contains a key and value. Something like this:
function parseParams(paramUnparsed){
var params = [];
for (var j = 0; j < paramUnparsed.length; j++) {
if (paramUnparsed[j].split('=').length < 2) {
params = undefined;
break;
}
var key = paramUnparsed[j].split('=')[0];
var value = paramUnparsed[j].split('=')[1];
params.push({ key: key, value: value });
}
// use like: `params[0].key` etc
return params;
}
Here is some thing odd. This actually holds together.
var arr = [];
arr["test"] = "something";
arr[arr.length]=arr["test"];
Now you have a key value array you kay loop through by key or by index. Also the length property is abailable.
本文标签: javascriptarray length with keyvaluejsStack Overflow
版权声明:本文标题:javascript - array length with key-value, js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740016851a2220588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论