简体   繁体   English

如何以正确的方式更新对象值

[英]How to update object value the right way

I am having issue updating my object's value. 我在更新object's值时遇到问题。

I have an object that is structured like this: 我有一个结构如下的对象:

const myObject =  {

      [_selectedDay] :
        {
        toDo:'workout',
        selected: true,
      }
  }

I have a button that adds my object and When I console.log my object I get this: 我有一个添加我的对象的按钮,当我console.log我的对象时,我得到这个:

//I expaned [0]
(5) [{…}, {…}, {…}, {…}, {…}] "object"
0: 
->2019-06-03: {toDo: "workout", selected: true}
1: {2019-06-04: {…}}
2: {2019-06-05: {…}}
3: {2019-06-06: {…}}
4: {2019-06-07: {…}}

My question: Is it possible to update 2019-06-03 's toDo field from workout to study? 我的问题:是否可以从workout到学习更新2019-06-03toDo字段?

I tried this code but won't work. 我尝试了此代码,但无法正常工作。

 myObject[0].toDo = 'workout'

So at first we should take a look at this: 因此,首先我们应该看一下:

(5) [{…}, {…}, {…}, {…}, {…}] "object"
0: 
->2019-06-03: {toDo: "workout", selected: true}
1: {2019-06-04: {…}}
2: {2019-06-05: {…}}
3: {2019-06-06: {…}}
4: {2019-06-07: {…}}

myObject[0].todo is undefined because there is no .todo on myObject[0] . myObject[0].todo是未定义的,因为myObject[0]上没有.todo Furthermore ther is no .todo anywhere in your object. 此外,在对象的任何地方都没有.todo But theres a .toDo (D instead of d) on myObject[0]['2019-06-03'] . 但是myObject[0]['2019-06-03']上有一个.toDo (用D代替d)。

using myObject[0]['2019-06-03'].toDo = 'study' should work. 使用myObject[0]['2019-06-03'].toDo = 'study'应该有效。 :) :)


Off-topic: (to answer your comment): 离题:(回答您的评论):

This could be a solution if your data-structure should not be changed. 如果您的数据结构不应该更改,这可能是一个解决方案。 Take a look at the comments inside the code: 看一下代码中的注释:

 var myObject = [ {'2019-06-04': {toDo: 'study', selected: true}}, {'2019-06-05': {toDo: 'workout', selected: true}}, {'2019-06-06': {toDo: 'party', selected: true}}, {'2019-06-07': {toDo: 'work', selected: true}} ]; function catchbydate(key) { // loop through your array: for(var i = 0; i < myObject.length; i++) { // return if objectkey was found: if(myObject[i].hasOwnProperty(key)) return myObject[i]; } // otherwise return null: return null; } console.log(catchbydate('2019-06-06')); 

You can try using the first key: 您可以尝试使用第一个键:

var firstKey = Object.keys(myObject)[0];
myObject[firstKey].todo = 'workout'

To answer your comment on J Sadi's answer, somehow in your code, your "myObject" is getting converted to array of objects and hence you have to access it via index, like myObject[0]. 为了回答您对J Sadi答案的评论,以某种方式在您的代码中,“ myObject”已转换为对象数组,因此您必须通过索引访问它,例如myObject [0]。 If your structure remains correct like below, you can directly access (or console.log) your property like myObject['2019-06-03']. 如果您的结构如下所示仍然正确,则可以像myObject ['2019-06-03']这样直接访问(或console.log)您的属性。

myObject =  {
      2019-06-03 :
        {
        toDo:'workout',
        selected: true,
      },
      2019-06-04 :
        {
        toDo:'study',
        selected: true,
      }
  }

now you can do console.log(myObject['2019-06-03']). 现在您可以执行console.log(myObject ['2019-06-03'])。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM