0

I have an Object with composition BDM. It includes an object inside an object.

I would like to update a value once the object has a specific string.

I can update the Object with the same value, however, I couldn't get the dynamic value with child object.

This is the Object I have.

    $data.timerObjJson; 

        [
          {
            "persistenceId": 322,
             "persistenceId_string": "322",
             "persistenceVersion": 0,
             "persistenceVersion_string": "0",
             "recommandation": "Recommandation  1",
                     "timerTask": [
                            {
                              "persistenceId": 508,
                              "persistenceId_string": "508",
                              "persistenceVersion": 0,
                              "persistenceVersion_string": "0",
                              "title": "Task1",
                              "startdate": "2019-02-13",
                              "enddate": "2019-02-13",
                              "statusTask": "decline",
                              "declineReason": ""
                            },
                            {
                              "persistenceId": 511,
                              "persistenceId_string": "508",
                              "persistenceVersion": 0,
                              "persistenceVersion_string": "0",
                              "title": "Task1",
                              "startdate": "2019-02-13",
                              "enddate": "2019-02-13",
                              "statusTask": "decline",
                              "declineReason": ""
                            }
                          ],
                          "statusRec": "In process"
                        },
                        {
                          "persistenceId": 323,
                          "persistenceId_string": "323",
                          "persistenceVersion": 0,
                          "persistenceVersion_string": "0",
                          "recommandation": "Recommandation 2",
                          "timerTask": [
                            {
                              "persistenceId": 509,
                              "persistenceId_string": "509",
                              "persistenceVersion": 0,
                              "persistenceVersion_string": "0",
                              "title": "Task2",
                              "startdate": "2019-02-13",
                              "enddate": "2019-03-02",
                              "statusTask": "decline",
                              "declineReason": ""
                            },
                            {
                              "persistenceId": 509,
                              "persistenceId_string": "509",
                              "persistenceVersion": 0,
                              "persistenceVersion_string": "0",
                              "title": "Task2",
                              "startdate": "2019-02-13",
                              "enddate": "2019-03-02",
                              "statusTask": "decline",
                              "declineReason": ""
                            }
                          ],
                          "statusRec": "In process"
                        }
                      ] 

This is what I tried to get return the statusTask and

    $data.getDone:
    var obj = [];
    for(var i in $data.timerObjJson){
        for(var j in $data.timerObjJson[i].timerTask){

            var thisObj = {};
            thisObj = $data.timerObjJson[i].timerTask[j]; 
            obj.push(thisObj);
        }
    } 

    var objArray=[];
            for (var i = 0; i < obj.length; i++) {    
             objArray.push(obj[i].statusTask); 
    }

   function isBelowThreshold(currentValue) {
      return currentValue == "done";
   }
   return objArray.every(isBelowThreshold)

And I tried to update the statusRec using by let as belows

      var data = $data.timerObjJson; 
      var done = $data.getDone; //true or false 

      let arr = data;

      const status = "In process";
      if (done == true){
           arr.find(v => v.statusRec == status).statusRec = "DONE";
       }
      return data;

But what I need to update is each statusRec depends on timerTask.statusTask's status.

For example, once all timerTask.statusTask changed "done" the statusRec should update to "Done".

1
  • All four timerTask.statusTask must be done? Or just groups of two? Commented Feb 13, 2019 at 21:08

2 Answers 2

1

This is a good question actually , if i understood the question correctly , you're looking for if timerTask.statusTask has value of 'done' of both 2 objects it changes statusRec to 'done' of the same object so you can reach that approach by using filter array method in JavaScript here is an example:

    let array = [
  {
    "persistenceId": 322,
     "persistenceId_string": "322",
     "persistenceVersion": 0,
     "persistenceVersion_string": "0",
     "recommandation": "Recommandation  1",
     "timerTask": [
        {
          "persistenceId": 508,
          "persistenceId_string": "508",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task1",
          "startdate": "2019-02-13",
          "enddate": "2019-02-13",
          "statusTask": "decline",
          "declineReason": ""
        },
        {
          "persistenceId": 511,
          "persistenceId_string": "508",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task1",
          "startdate": "2019-02-13",
          "enddate": "2019-02-13",
          "statusTask": "decline",
          "declineReason": ""
        }
      ],
      "statusRec": "In process"
    },
    {
      "persistenceId": 323,
      "persistenceId_string": "323",
      "persistenceVersion": 0,
      "persistenceVersion_string": "0",
      "recommandation": "Recommandation 2",
      "timerTask": [
        {
          "persistenceId": 509,
          "persistenceId_string": "509",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task2",
          "startdate": "2019-02-13",
          "enddate": "2019-03-02",
          "statusTask": "decline",
          "declineReason": ""
        },
        {
          "persistenceId": 509,
          "persistenceId_string": "509",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task2",
          "startdate": "2019-02-13",
          "enddate": "2019-03-02",
          "statusTask": "done",
          "declineReason": ""
        }
      ],
      "statusRec": "In process"
    }
]


function TheSameInAllObjects(status) {
  if (!(status && status.length)) return true; // If there is no status, or if it is empty, they are all the same, aren't they?
  let compare = status[0].statusTask;
  return status.every( item => item.statusTask === compare);
}

array.filter(data => {
   let obj = {}
   data['timerTask'].filter(result => {
    if(result.statusTask === 'done'){
      obj = data
      if(TheSameInAllObjects(obj['timerTask']) === true){
        return data.statusRec = 'done'
      }
    }
  })
})

console.log(array)

and you can check here for both has the same value of 'done'

let array = [
  {
    "persistenceId": 322,
     "persistenceId_string": "322",
     "persistenceVersion": 0,
     "persistenceVersion_string": "0",
     "recommandation": "Recommandation  1",
     "timerTask": [
        {
          "persistenceId": 508,
          "persistenceId_string": "508",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task1",
          "startdate": "2019-02-13",
          "enddate": "2019-02-13",
          "statusTask": "decline",
          "declineReason": ""
        },
        {
          "persistenceId": 511,
          "persistenceId_string": "508",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task1",
          "startdate": "2019-02-13",
          "enddate": "2019-02-13",
          "statusTask": "decline",
          "declineReason": ""
        }
      ],
      "statusRec": "In process"
    },
    {
      "persistenceId": 323,
      "persistenceId_string": "323",
      "persistenceVersion": 0,
      "persistenceVersion_string": "0",
      "recommandation": "Recommandation 2",
      "timerTask": [
        {
          "persistenceId": 509,
          "persistenceId_string": "509",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task2",
          "startdate": "2019-02-13",
          "enddate": "2019-03-02",
          "statusTask": "done",
          "declineReason": ""
        },
        {
          "persistenceId": 509,
          "persistenceId_string": "509",
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "title": "Task2",
          "startdate": "2019-02-13",
          "enddate": "2019-03-02",
          "statusTask": "done",
          "declineReason": ""
        }
      ],
      "statusRec": "In process"
    }
]


function TheSameInAllObjects(status) {
  if (!(status && status.length)) return true; // If there is no status, or if it is empty, they are all the same, aren't they?
  let compare = status[0].statusTask;
  return status.every( item => item.statusTask === compare);
}

array.filter(data => {
   let obj = {}
   data['timerTask'].filter(result => {
    if(result.statusTask === 'done'){
      obj = data
      if(TheSameInAllObjects(obj['timerTask']) === true){
        return data.statusRec = 'done'
      }
    }
  })
})

console.log(array)

i have updated my answer to achieve your goal

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

5 Comments

Thanks a lot. I think it is almost what I want. Then, is it possible to update the statusRec as "done" when all statusTask are done. In the example, if one of the value is done the statusRec updated "done". I would like to update once all statusTask are "done". I will try to update the value based on your code. Thanks a lot
no problem happy to help , you can use the same filter method and to return the desired object and do some extra operations to reach your goal
I am in lost while I tried to create the operation. Would you add the code if possible? Thanks in advanced. I tried to do, but I cannot get the statusTasks of each object. function isBelowThreshold(currentValue) { return currentValue === "done"; } var array1 = ["done","done"] // get statusTasks array.filter(data => { data["timerTask"].filter(result => { var result1 = result.statusTask; //if(result.statusTask === "done"){ if(array1.every(isBelowThreshold)){ return data.statusRec = "Done" ; } }); }); //console.log(array)
@Jane i have updated my answer to answer your question
Thanks a lot!!! This works perfectly. The code looks still difficult for me, however, I will try to understand. I appreciate your help. Regards, :)
1

this is how you can update an object:

var object = {
  "name": "aziz",
  "address": {
    "ville": "MTL",
    "street": {
      "name": "Bonnier",
      "number": 8012
    }
  }
}


object = { ...object,
  address: { ...object.address,
    street: {
      ...object.address.street,
      number: 9000
    }
  }
}

console.log(object);

var object2 = {
  "name": "aziz",
  "address": {
    "ville": "MTL",
    "street": {
      "name": "Bonnier",
      "number": 8012
    }
  }
}


/* 2 method */
object2.address.street.number = 10000;

console.log(object2);

4 Comments

Thanks for your comment, then my object has another object inside. I couldn't adjust the code you provided. Could you give me more idea about that? Thanks in advanced.
Updated with an object inside another object
I am sorry to bother you again, In my case, I need to update object.name once all address.number changed specific value for example 8022
sorry i didn't get what you want i ll delete my answer

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.