我正在使用二维数组来存储问题及其答案。我遇到的问题是我应该将值推入其中的方式
Cannot read property 'push' of undefined
var answers = [];
answers[0].push({"value":true, "answer":"4"});
answers[0].push({"value":false, "answer":"3"});
answers[0].push({"value":false, "answer":"2"});
answers[0].push({"value":false, "answer":"1"});
如何将这些答案(值和答案文本)链接到代表问题编号的特定索引?
答案 0 :(得分:3)
目前,您的read -d '' SQL_QUERY_1 << EOF
SELECT prod.id as id, prod.title as title, comp.name as company, pho.id as photo_id, pho.image as photo_name
FROM products as prod
JOIN accounts as comp
ON comp.id = prod.account_id
JOIN photos as pho
ON pho.id = prod.featured_photo_id;
EOF
echo ${SQL_QUERY_1} | mysql
已编入索引的项目为0
。首先初始化您的undefined
索引项以引用数组,然后在其上调用0
push
答案 1 :(得分:1)
你设置它的方式,答案只是一维数组。 但是,如果你想存储问题和答案,我认为一维数组就足够了:
var answers = [];
answer.push(
{
question: "question string",
answer: "answer string"
});
在这个具体示例中,您应该能够使用
来解决问题answers[0].question
或以
回答answers[0].answer
希望它有所帮助! :d
答案 2 :(得分:0)
您没有元素,答案[0]为空。尝试添加 answers.push([]) 在推动其他人之前