[英]Is this a valid Javascript object?
Is this a valid javascript object? 这是有效的javascript对象吗?
var sections =
{
{ name: 'SERVER', isKeyValuePair: true },
{ name: 'TYPES', isKeyValuePair: false },
{ name: 'USERS', isKeyValuePair: false }
};
How would I access this? 我将如何访问?
I will know the name ('SERVER' etc) and will want to get the true or false value for the isKeyValuePair property. 我将知道名称(“ SERVER”等),并希望获取isKeyValuePair属性的true或false值。
Is there a better layout for this object whereby I can still access the keyvaluepair property based on a search of the name? 这个对象是否有更好的布局,使我仍然可以基于名称搜索来访问keyvaluepair属性?
Thanks a lot 非常感谢
Joe 乔
Is this a valid javascript object? 这是有效的javascript对象吗?
No. Inside an object literal you need to have key: value
pairs. 否。在对象文字中,您需要具有key: value
对。
Is there a better layout for this object whereby I can still access the keyvaluepair property based on a search of the name? 这个对象是否有更好的布局,使我仍然可以基于名称搜索来访问keyvaluepair属性?
This would be valid: 这将是有效的:
var sections =
[
{ name: 'SERVER', isKeyValuePair: true },
{ name: 'TYPES', isKeyValuePair: false },
{ name: 'USERS', isKeyValuePair: false }
];
In which case you could loop over the array until you found the entry you wanted. 在这种情况下,您可以遍历数组,直到找到所需的条目。
You'd probably want to express it in this way through: 您可能希望通过以下方式表达它:
var sections =
{
SERVER: true,
TYPES: false,
USERS: false
};
Then you would just sections.SERVER
or sections['SERVER']
(and thus also var section = "SERVER"; sections[section];
since you might have the key name in a variable) 然后,您将只使用sections.SERVER
或sections['SERVER']
(因此也可以使用var section = "SERVER"; sections[section];
因为您可能在变量中包含键名)
No, it isn't. 不,不是。 You should suplly the inner objects with keys, or us an array of objects: 您应该为内部对象提供键,或者给我们一个对象数组:
// key-value pairs
var sections =
{
o1: { name: 'SERVER', isKeyValuePair: true },
o2: { name: 'TYPES', isKeyValuePair: false },
o3: { name: 'USERS', isKeyValuePair: false }
};
console.log(sections.o1.name); //=> 'SERVER'
// array
var sections =
[
{ name: 'SERVER', isKeyValuePair: true },
{ name: 'TYPES', isKeyValuePair: false },
{ name: 'USERS', isKeyValuePair: false }
];
console.log(sections[0].name); //=> 'SERVER'
This is not valid. 这是无效的。
You can use either 您可以使用
var sections =
{
s1: { name: 'SERVER', isKeyValuePair: true },
s2: { name: 'TYPES', isKeyValuePair: false },
s3: { name: 'USERS', isKeyValuePair: false }
};
and acces as sections.s1
, ... 和ACCES为sections.s1
,...
or 要么
var sections =
[
{ name: 'SERVER', isKeyValuePair: true },
{ name: 'TYPES', isKeyValuePair: false },
{ name: 'USERS', isKeyValuePair: false }
];
and sections[0]
, ... 和sections[0]
,...
No, it isn't a valid object.If it was structured like this: 不,它不是有效的对象。如果它的结构如下:
var sections = {
prop11: { name: 'SERVER', isKeyValuePair: true },
prop2: { name: 'TYPES', isKeyValuePair: false },
prop3: { name: 'USERS', isKeyValuePair: false }
};
It would be correct. 这是正确的。 However it looks like what you really want is an array. 但是,看起来您真正想要的是一个数组。 Try using the following: 尝试使用以下内容:
var sections =
[
{ name: 'SERVER', isKeyValuePair: true },
{ name: 'TYPES', isKeyValuePair: false },
{ name: 'USERS', isKeyValuePair: false }
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.