admin管理员组文章数量:1425879
I have a JavaScript object which includes an array. I created a new key, length
, which is supposed to store the value of the length of the fields
array.
$(document).ready(function() {
var data = {
"label": "Information",
"fields": [
{
"name": "name",
"label": "Team Name",
"type": "string",
"config": {}
}
],
"length": fields.length // Need help with this line
};
$("button").click(function() {
alert(data.length);
});
});
/
In my fiddle, nothing gets alerted, although it should alert 1, since there is 1 record in the fields array.
Any ideas?
Edit
I know you can get the length OUTSIDE of the object by creating a new variable
var length = data.fields.length;
However, I need the length to be inside of the object.
I have a JavaScript object which includes an array. I created a new key, length
, which is supposed to store the value of the length of the fields
array.
$(document).ready(function() {
var data = {
"label": "Information",
"fields": [
{
"name": "name",
"label": "Team Name",
"type": "string",
"config": {}
}
],
"length": fields.length // Need help with this line
};
$("button").click(function() {
alert(data.length);
});
});
http://jsfiddle/kvtywmtm/
In my fiddle, nothing gets alerted, although it should alert 1, since there is 1 record in the fields array.
Any ideas?
Edit
I know you can get the length OUTSIDE of the object by creating a new variable
var length = data.fields.length;
However, I need the length to be inside of the object.
Share Improve this question edited Jul 14, 2015 at 16:34 Richard Hamilton asked Jul 14, 2015 at 16:28 Richard HamiltonRichard Hamilton 26.5k11 gold badges65 silver badges88 bronze badges 5-
2
You have no
fields
variable so you'll get a ReferenceError. You can't just use an identifier and hope the language will understand what you want. – user1106925 Commented Jul 14, 2015 at 16:28 - Then how can I reference the length of that field in my object? – Richard Hamilton Commented Jul 14, 2015 at 16:29
-
Why not
alert(data.fields.length)
? – Andreas Commented Jul 14, 2015 at 16:30 -
@RichardHamilton: Depends. Do you want only the initial value? Or do you want it always to represent the current
length
of the array? You say you want to "store" it, so it doesn't sound like you always want the current length. – user1106925 Commented Jul 14, 2015 at 16:33 -
I actually have an array containing many of these objects. I need to know the length of the
fields
array that's in each of these so I can add custom code. This is just a small example of the code – Richard Hamilton Commented Jul 14, 2015 at 16:39
3 Answers
Reset to default 6You can use a getter
$(document).ready(function() {
var data = {
"label": "Information",
"fields": [{
"name": "name",
"label": "Team Name",
"type": "string",
"config": {}
}],
get length() {
return this.fields.length
}
};
$("button").click(function() {
console.log(data.length);
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<button>Click Here</button>
var data = {
"label": "Information",
"fields": [
{
"name": "name",
"label": "Team Name",
"type": "string",
"config": {}
}
],
};
data.length = fields.length;
This won't auto-update the length
field, but it doesn't seem as if you need it to.
Try this JSFiddle
I don't know what length you need in the alert, but if you need the object data
properties length you need the following. Also, to pass fields
length you need to add it after declare the array:
JS:
$(document).ready(function() {
var data = {
"label": "Information",
"fields": [
{
"name": "name",
"label": "Team Name",
"type": "string",
"config": {}
}
]
};
data.length = data.fields.length
$("button").click(function() {
alert(Object.keys(data).length);
});
});
本文标签: javascriptGetting the length of an array inside an objectStack Overflow
版权声明:本文标题:javascript - Getting the length of an array inside an object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745424986a2658068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论