Reputation: 2572
I have an array and inside each array is a json object with each day of the week, so my array would look something like this:
var array = [
{
"wednesday":{
"notes":"some notes for Wednesday"
},
},
{
"thursday":{
"notes":"some notes for Thursday"
}
}
];
i can get away with updating my object directly by calling the following:
array[0].wednesday.notes = "updating Wednesday notes";
However, I need to update it dynamically....
I have a function that looks something like this, I need to dynamically call the day of the week on my json object and not be locked into just wednesday, i need to be able to call wednesday, thursday, friday etc on my object, how can i do this?
function updateObject(index, empNum) {
console.log(index+", "+empNum)
array[index].employee = $("#employee_" + empNum).val();
array[index].wednesday.notes = $("#employee_" + empNum + "_wed_notes").val();
array[index].wednesday.start = $("#employee_" + empNum + "_wed_shift_start").val();
array[index].wednesday.lunch = $("#employee_" + empNum + "_wed_lunch").val();
array[index].wednesday.end = $("#employee_" + empNum + "_wed_shift_end").val();
array[index].wednesday.short_day = $("#employee_" + empNum + "_wed_short_day").is(':checked');
array[index].wednesday.lack_of_work = $("#employee_" + empNum + "_wed_lack_of_work").is(':checked');
array[index].wednesday.full_day = $("#employee_" + empNum + "_wed_full_day").is(':checked');
var row_count = $("input[id*='employee_" + empNum + "_wed_job_']").length;
for (var i = 0; i < row_count; i++) {
var data = {};
data.job = $("input[id*='employee_" + empNum + "_wed_job_']").eq(i).val();
data.hrs = $("input[id*='employee_" + empNum + "_wed_hrs_']").eq(i).val();
data.cost_code = $("input[id*='employee_" + empNum + "_wed_cost_code_']").eq(i).val();
data.st = $("input[id*='employee_" + empNum + "_wed_st_']").eq(i).is(':checked');
data.ot = $("input[id*='employee_" + empNum + "_wed_ot_']").eq(i).is(':checked');
data.dt = $("input[id*='employee_" + empNum + "_wed_dt_']").eq(i).is(':checked');
array[index].wednesday.data[i] = data;
}
}
i tried something like doing array[index].[thursday].notes = "x";
but unfortunately that doesnt work, i need to be able to call the day of the week i need when i call the function
so i need it to be something like updateObject(2,1,"thursday");
Upvotes: 0
Views: 215
Reputation: 8921
You can access all your data as so:
const updateObject = (index, empNum) => {
const i = array[index], k = Object.keys(i)[0]
if (!k) {return console.error("Invalid Data at index",index)}
i[k].notes = `Whatever you want with ${empNum}`
}
The function isolates the key given at a certain location and accesses it.
Example: updateObject(0, "15 employees")
If you would rather have ^^ do it by day then your function would look like:
const updateObject = (day, empNum) => {
const i = array.map(r => {
const k = Object.keys(r)[0];if (!k) {return false}
return r[k]
}).filter(r => r)[0]
if (!i) {return console.error("Invalid Day [%s] provided",day)}
i.notes = `Whatever you want with ${empNum}`
}
Not you can use it like: updateObject('tuesday', "15 employees")
Upvotes: 0
Reputation: 1976
You just need to use the bracket notation to access the correct element in your array/objects.
This function would let you enter the week number (array index) as well as the day you want to update.
var array = [
{
"wednesday":{
"notes":"some notes for Wednesday"
},
},
{
"thursday":{
"notes":"some notes for Thursday"
}
}
];
function updateArray(index, day, newNotes) {
array[index][day].notes = newNotes;
}
console.log('before', array);
updateArray(1, 'thursday', 'updated notes');
console.log('after', array);
Upvotes: 1