我有一个包含BDM的对象。 它在对象内部包括一个对象。
一旦对象具有特定的字符串,我想更新一个值。
我可以使用相同的值更新对象,但是,我无法通过子对象获得动态值。
这是我拥有的对象。
$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"
}
]
这就是我试图返回statusTask和
$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)
然后我尝试通过let来更新statusRec
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;
但是我需要更新的是每个statusRec取决于timerTask.statusTask的statuse。
例如,一旦所有timerTask.statusTask更改为“完成” statusRec应该更新为“完成”。
对不起,这个问题很复杂。 任何帮助将不胜感激。
非常感谢。
答案 0 :(得分:1)
这是更新对象的方法:
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);
答案 1 :(得分:1)
实际上这是一个好问题,如果我正确理解了这个问题,则您正在寻找timerTask.statusTask是否具有两个对象的“ done”值,它将statusRec更改为同一对象的“ done”,因此您可以通过使用JavaScript中的过滤器数组方法达到该方法,下面是一个示例:
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)
,您可以在此处检查两者是否具有相同的'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)
我已更新答案以实现您的目标