基本上,我有以下数组(或JSON):
apps = {
app1:
[
"ci-extension",
"Unnamed",
"<h1>Hello world!</h1>"
],
app2:
[
"ci-extension",
"Another one!",
"Cool!"
],
}
有没有办法确定阵列有多少个孩子?
例如。在这种情况下,答案是2
: app1 &amp;的 APP2 即可。如果我们再添加一个对象&#34; 应用程序&#34;答案是3
。我尝试了以下方法:
apps.length
//returns "undefined"
for (i = 0; apps[i] != undefined; i++) {
console.log(apps[i]);
//also returns "undefined"
//I am later gonna use also the values in the child, so I stored all the children to a new array
meta.push(apps[i]); //"meta" here is another vriable with an array in it
}
(如果这有意义的话)
我希望for
返回这样的内容:
APP1
app2
应用(如果您计算我们添加的新对象)
meta
的值为["app1", "app2", "application"]
。
我的思绪完全被混淆了。我不知道我在哪里做错了什么,所以任何帮助都会受到高度赞赏。提前谢谢!
修改
如果有push()
元素进入apps
的某种方式,我会请你点亮我。再次感谢!
答案 0 :(得分:0)
您可以执行以下操作
let apps = {
app1:
[
"ci-extension",
"Unnamed",
"<h1>Hello world!</h1>"
],
app2:
[
"ci-extension",
"Another one!",
"Cool!"
],
}
let result = Object.keys(apps).length;
console.log(result);
&#13;
中的键总数
您可以通过以下方式为此对象指定属性
let apps = {
app1:
[
"ci-extension",
"Unnamed",
"<h1>Hello world!</h1>"
],
app2:
[
"ci-extension",
"Another one!",
"Cool!"
],
}
apps['app3'] = ["test"];
let result = Object.keys(apps).length;
console.log(result);
// or the following way
let objectToPush = {app4: ['test2']};
Object.assign(apps, objectToPush);
console.log(Object.keys(apps));
console.log(Object.keys(apps).length);
&#13;