最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the length of an array inside an object - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

You 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 fieldslength 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);
    });
});
发布评论

评论列表(0)

  1. 暂无评论