درود
من یک آرایه تودرتو مانند مثال زیر دارم . بهترین راه برای بروز رسانی answer با ID شماره 2 در مقیاس بزرگ چی می تونه باشه؟
ممنون از راهنماییتون
map()? forEach()? find()? indexOf()?
[
{
"id": 1,
"question": "this is the question",
"tagify": "#yes",
"answers": [
{
"id": 1,
"quizID": 1,
"answer": "the new answer",
"correctAnswer": 0
},
{
"id": 2,
"quizID": 1,
"answer": "the new answer 2",
"correctAnswer": 1
},
{
"id": 3,
"quizID": 1,
"answer": "The third answer",
"correctAnswer": 0
},
{
"id": 4,
"quizID": 1,
"answer": "it's a Gadget that facilitate communication",
"correctAnswer": 1
}
]
},
{
"id": 2,
"question": "this is the second question",
"tagify": "#Second",
"answers": [
{
"id": 5,
"quizID": 2,
"answer": "and it also is right answer",
"correctAnswer": 0
},
{
"id": 6,
"quizID": 2,
"answer": "this is the wrong answer",
"correctAnswer": 1
},
{
"id": 7,
"quizID": 2,
"answer": "this is the right answer",
"correctAnswer": 0
},
{
"id": 245,
"quizID": 2,
"answer": "The first Answer3",
"correctAnswer": 1
},
{
"id": 5176,
"quizID": 2,
"answer": "that's the new one",
"correctAnswer": 0
}
]
}
] const array1 = [
{
"id": 1,
"question": "this is the question",
"tagify": "#yes",
"answers": [
{
"id": 1,
"quizID": 1,
"answer": "the new answer",
"correctAnswer": 0
},
{
"id": 2,
"quizID": 1,
"answer": "the new answer 2",
"correctAnswer": 1
},
{
"id": 3,
"quizID": 1,
"answer": "The third answer",
"correctAnswer": 0
},
{
"id": 4,
"quizID": 1,
"answer": "it's a Gadget that facilitate communication",
"correctAnswer": 1
}
]
},
{
"id": 2,
"question": "this is the second question",
"tagify": "#Second",
"answers": [
{
"id": 5,
"quizID": 2,
"answer": "and it also is right answer",
"correctAnswer": 0
},
{
"id": 6,
"quizID": 2,
"answer": "this is the wrong answer",
"correctAnswer": 1
},
{
"id": 7,
"quizID": 2,
"answer": "this is the right answer",
"correctAnswer": 0
},
{
"id": 245,
"quizID": 2,
"answer": "The first Answer3",
"correctAnswer": 1
},
{
"id": 5176,
"quizID": 2,
"answer": "that's the new one",
"correctAnswer": 0
}
]
}
];
function updateQuestion(arr, questionID, answerID, answerString) {
const foundQuestionIndex = arr.findIndex(element => element.id === questionID);
const arrAnswers = arr[foundQuestionIndex].answers;
const foundAnswerIndex = arrAnswers.findIndex(element => element.id === answerID);
arr[foundQuestionIndex]["answers"][foundAnswerIndex]["answer"] = answerString;
}
updateQuestion(array1, 2, 245, "new answer");
console.log(array1);مقادیر داخل آرایه تون مرتب هستش؟
یعنی مثلا 1 تا 50، شامل سوالات 1 تا 50 میشه؟ همه موجود هستند و به ترتیب هستند؟ یا اینکه برخی مقادیر ممکنه موجود نباشند؟
شاید پاسخ شما همینجا باشد
برخی مقادیر ممکنه موجود نباشه، یعنی توسط کاربر پاک شه.
یه سوال بنیادی هم بپرسم، اینکه اصولا اینطور واکشی از سرور استاندارد هستش؟ یا باید جدا از هم این جداول Question و Answer رو واکشی کنم؟
متشکرم
@milad
const array1 = [
{
"id": 1,
"question": "this is the question",
"tagify": "#yes",
"answers": [
{
"id": 1,
"quizID": 1,
"answer": "the new answer",
"correctAnswer": 0
},
{
"id": 2,
"quizID": 1,
"answer": "the new answer 2",
"correctAnswer": 1
},
{
"id": 3,
"quizID": 1,
"answer": "The third answer",
"correctAnswer": 0
},
{
"id": 4,
"quizID": 1,
"answer": "it's a Gadget that facilitate communication",
"correctAnswer": 1
}
]
},
{
"id": 2,
"question": "this is the second question",
"tagify": "#Second",
"answers": [
{
"id": 5,
"quizID": 2,
"answer": "and it also is right answer",
"correctAnswer": 0
},
{
"id": 6,
"quizID": 2,
"answer": "this is the wrong answer",
"correctAnswer": 1
},
{
"id": 7,
"quizID": 2,
"answer": "this is the right answer",
"correctAnswer": 0
},
{
"id": 245,
"quizID": 2,
"answer": "The first Answer3",
"correctAnswer": 1
},
{
"id": 5176,
"quizID": 2,
"answer": "that's the new one",
"correctAnswer": 0
}
]
}
];
const foundIndex = array1.findIndex(element => element.id === 2);
array1[foundIndex].question = "new Question";
console.log(array1);متشکرم
فقط اینکه من می خوام یکی از answer های مربوط به یکی از Question هارو آپدیت کنم.
در واقع هم آی دی Question و هم آی دی Answer رو هم دارم.
آی دی Answer ها هم یونیک هستند.
@milad
این کد باید درست کار کنه،
جزئیات بیشترش با خودتون.
درود و خسته نباشید، زیاد ریز سوالت نشدم ولی عنوان رو دیدم جوابی که تو ذهنم هست رو میگم شاید برات کاربردی باشه، شرکت ما از یک کتابخونه برای لود منوهای تودرتوش استفاده میکنه به نام fancytree که با jquery اجرا میشه همچنین یک حالت داره که زیرمنو رو وقتی بازکردی تازه میره اطلاعات رو با یک درخواست ajax میگیره و زیرمنوها رو تولید میکننه
همچنین پشتیبانی خوبی داره داکیومنتش که نه، ولی مثال برای انواعش زیاد پیدا میشه، استفاده ازش راحته