簡體   English   中英

如何動態更新數組中另一個對象內的嵌套對象?

[英]how can i dynamically update my nested object within another object, within an array?

我有一個數組,每個數組內部每個星期的每一天都是一個json對象,所以我的數組看起來像這樣:

var array = [
    {
        "wednesday":{
            "notes":"some notes for Wednesday"
        },
    },
    {
        "thursday":{
            "notes":"some notes for Thursday"
        }
    }
];

我可以通過調用以下命令直接更新對象:

array[0].wednesday.notes = "updating Wednesday notes";

但是,我需要動態更新...。

我有一個看起來像這樣的函數,我需要在我的json對象上動態調用星期幾,而不是僅在星期三被鎖定,我需要能夠在我的對象上調用星期三,星期四,星期五等,如何我可以這樣做嗎?

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

我試過做array [index]。[thursday] .notes =“ x”;

但不幸的是,這行不通,我需要能夠在調用函數時調用所需的星期幾

所以我需要它像updateObject(2,1,"thursday");

您只需要使用括號符號即可訪問數組/對象中的正確元素。

此功能可讓您輸入星期數(數組索引)以及要更新的日期。

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

您可以這樣訪問所有數據:

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}`
}

該功能隔離在特定位置給出的密鑰並對其進行訪問。

示例: updateObject(0, "15 employees")

如果您希望每天都進行^^操作,則您的函數應如下所示:

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}`
}

不是,您可以像這樣使用它: updateObject('tuesday', "15 employees")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM