繁体   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