0

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");

8
  • 1
    You seem to already know how to use bracket notation, and adding another argument to the function should be trivial ? Commented Dec 9, 2016 at 20:43
  • unfortunately i dont know how to use bracket notation, or at least im not clear on what that is =/ Commented Dec 9, 2016 at 20:43
  • jsfiddle.net/vjon7k13 Commented Dec 9, 2016 at 20:45
  • i'm a bit confused, does each object in your array contain only one day? if so why not use an object instead of an array? Commented Dec 9, 2016 at 20:45
  • @Punit no it doesnt just contain one day, it contains a full week, that was my error on how i typed up my question Commented Dec 9, 2016 at 20:47

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

5 Comments

No need to pass both index and day, passing one will allow you to infer the other.
@StephanBijzitter that's not true. have another look at the array structure. each element in the array is an object that could have multiple days.
You can use ES6 Filters to do it for you as well!
@AndrewIce OP specified in this comment stackoverflow.com/questions/41068528/…, that each object contains a full week, therefore multiple days.
Comments can be deleted.
0

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")

1 Comment

I doubt the asker would know how to transpile this code, given this question. Good solution, though. The arrow function however is simply clutter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.