我希望我的变量数组(在JavaScript中)具有2个值:在其上加引号,以及一个true或false值。
这是可放入以下代码的一部分:
var q = new Array()
q[0]='There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>'
q[1]='Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>'
q[2]='I know of no more encouraging fact than the unquestionable ability of man to elevate his life by conscious endeavor. <i>-Henry David Thoreau</i>'
q[3]='Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>'
那是我引用的众多语录之一(很快就会被琐事,我从另一个站点借来了一些代码以随机生成其中之一。)
例如,我希望q [3]是一个引号和一个true或false值。
这可能吗?关于我应该如何做的任何建议?
我是一个初学者,所以很抱歉,如果这是一个明显的问题。
最佳答案
您可以使用带有属性的对象文字来保留引号,使用另一个属性来保留 bool(boolean) 值。因此,例如:
var q = []; // NEVER use new Array() and ALWAYS put a semicolon at the end of lines.
q[0] = {
quote: 'There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>',
someValue: true
};
// ...
alert(q[0].quote); // There are some people...
alert(q[0].someValue); // true