变量数组中的对象是否有可能有2个值?

时间:2012-02-09 00:51:48

标签: javascript arrays variables object

我希望我的变量数组(在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]为引用为真或假值。

这可能吗?关于我应该怎么做的任何建议呢?

我是一名初学者,如果这是一个明显的问题,那就很抱歉。

7 个答案:

答案 0 :(得分:4)

您可以使用带有属性的对象文字来保存引号,使用另一个属性来保存布尔值。所以,例如:

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

答案 1 :(得分:2)

好的,如果我关注你,你想要的是一系列对象:

[{flag: true, text: "If you choose the red pill..."},...]

这有意义吗?

关键是你需要在数组的每个元素上都有一个JS对象。

答案 2 :(得分:1)

使其成为一个数组。

q[3] = ['My string....', true];

然后使用q[3][0]访问“我的字符串....”和q[3][1]以访问布尔值。


作为旁注,在创建数组时,您应该使用简写[]表示法而不是new Array()

var q = [];

答案 3 :(得分:1)

使用嵌套数组。

q[0] = ['Some quote',true];

然后q[0][0]是引用,q[0][1]是真/假值。

答案 4 :(得分:1)

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>', true]

q[1]=['Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>', false]

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>', true]

q[3]=['Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>', false]

if (q[3][1]) {
    print q[3][0]
}

答案 5 :(得分:1)

我可能会使用一个对象文字。类似的东西:

var q = [];

q[0]= {Question: '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>', Answer: true};
q[1]= {Question: 'Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>', Answer: true};
q[2]= {Question: '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>', Answer: false};
q[3]= {Question: 'Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>', Answer: false};

window.alert(q[1].Question);
window.alert(q[1].Answer);

答案 6 :(得分:1)

这样做的方法很多,这里有三个:

var q = [];
q.push("Quote#1");
q.push(true);
q.push("Quote#2");
q.push(false);

for(var i = 0; i < q.length-1; i++) {
    console.log(q[i], q[i+1]);
}

var q = [];
q.push({quote: "Quote#1", flag: true});
q.push({quote: "Quote#2", flag: false});
for (var i = 0; i < q.length; i++) {
    console.log(q[i].quote, q[i].flag);
}

var q = [];
q.push(["Quote#1", true]);
q.push(["Quote#2", false]);
for (var i = 0; i < q.length; i++) {
    console.log(q[i][0], q[i][1]);
}