درود
من یک آرایه تودرتو مانند مثال زیر دارم . بهترین راه برای بروز رسانی 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 میشه؟ همه موجود هستند و به ترتیب هستند؟ یا اینکه برخی مقادیر ممکنه موجود نباشند؟
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);
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);
درود و خسته نباشید، زیاد ریز سوالت نشدم ولی عنوان رو دیدم جوابی که تو ذهنم هست رو میگم شاید برات کاربردی باشه، شرکت ما از یک کتابخونه برای لود منوهای تودرتوش استفاده میکنه به نام fancytree که با jquery اجرا میشه همچنین یک حالت داره که زیرمنو رو وقتی بازکردی تازه میره اطلاعات رو با یک درخواست ajax میگیره و زیرمنوها رو تولید میکننه
همچنین پشتیبانی خوبی داره داکیومنتش که نه، ولی مثال برای انواعش زیاد پیدا میشه، استفاده ازش راحته
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟