简体   繁体   English

根据条件将 object 复制到另一个

[英]Copy an object into another based on condition

lets say I have this sample object like below,假设我有这个示例 object,如下所示,

let parentObj = {
  "SM": {
    "child_roles": {
      "SM#02": {
        "code": "SM#02",
        "assigned_info": [
          {
            "cur_assigned_to": "7486",
            "assigned_date": 1644483524,
            "assigned_fromdate": 1644517800,
            "assigned_todate": 1645122600,
          },
          {
            "cur_assigned_to": "3050",
            "assigned_date": 1656613800,
            "assigned_fromdate": 1658255400,
            "assigned_todate": 1659292200,
          }
        ]
      },
      "SM#03": {
        "code": "SM#03",
        "assigned_info": [
          {
            "cur_assigned_to": "7486",
            "assigned_date": 1644483638,
            "assigned_fromdate": 1644517800,
            "assigned_todate": 1645727400,
          },
          {
            "cur_assigned_to": "3050",
            "assigned_date": 1656613800,
            "assigned_fromdate": 1656613800,
            "assigned_todate": 1660069800,
          }
        ]
      },
      "SM#01": {
        "code": "SM#01",
        "assigned_info": []
      }
    }
  },
  "SMP": {
    "child_roles": {
      "SMP#01": {
        "code": "SMPR#01",
        "assigned_info": [
          {
            "cur_assigned_to": "7486",
            "assigned_date": 1644483672,
            "assigned_fromdate": 1644431400,
            "assigned_todate": 1645122600,
          }
        ]
      }
    }
  },
  "SMR": {
    "child_roles": {
      "SMR#01": {
        "code": "SMOR#01",
        "assigned_info": []
      },
      "SMOR#02": {
        "code": "SMOR#02",
        "assigned_info": []
      }
    }
  },
  "ANC": {
    "child_roles": {}
  }
}

Now there is a key named assigned_info, this is a collection of assignment information.现在有一个名为assigned_info的键,这是一个赋值信息的集合。 I want to check if it contains an item whose cur_assigned_to = 3050 then copy that array item along with other properties of that object.我想检查它是否包含 cur_assigned_to = 3050 的项目,然后复制该数组项目以及该 object 的其他属性。 Sorry for my bad English, in another word, I want to get this output like below,对不起我的英语不好,换句话说,我想得到这个 output 如下所示,

let expectedObj = {
  "SM": {
    "child_roles": {
      "SM#02": {
        "code": "SM#02",
        "assigned_info": [
          {
            "cur_assigned_to": "3050",
            "assigned_date": 1656613800,
            "assigned_fromdate": 1658255400,
            "assigned_todate": 1659292200,
          }
        ]
      },
      "SM#03": {
        "code": "SM#03",
        "assigned_info": [
          {
            "cur_assigned_to": "3050",
            "assigned_date": 1656613800,
            "assigned_fromdate": 1656613800,
            "assigned_todate": 1660069800,
          }
        ]
      },
      "SM#01": {
        "code": "SM#01",
        "assigned_info": []
      }
    }
  },
  "SMP": {
    "child_roles": {
      "SMP#01": {
        "code": "SMPR#01",
        "assigned_info": [
        ]
      }
    }
  },
  "SMR": {
    "child_roles": {
      "SMR#01": {
        "code": "SMOR#01",
        "assigned_info": []
      },
      "SMOR#02": {
        "code": "SMOR#02",
        "assigned_info": []
      }
    }
  },
  "ANC": {
    "child_roles": {}
  }
}

What I have tried: I can loop through all the elements of that assigned_info array and check whether the cur_assigned_to is 3050, but I cannot able to copy the remaining keys and couldn't able to delete other array elements.我已经尝试过:我可以遍历该assigned_info 数组的所有元素并检查cur_assigned_to 是否为3050,但我无法复制剩余的键并且无法删除其他数组元素。

You can copy the entire parentObj and remove unexpected items in array.您可以复制整个parentObj并删除数组中的意外项目。

let objCopy = JSON.parse(JSON.stringify(parentObj))
for (let i in objCopy) {
    for (let j in objCopy[i]['child_roles']) {
        let obj = objCopy[i]['child_roles'][j]['assigned_info']
        for (let k in obj) {
            if ('cur_assigned_to' in obj[k] && obj[k].cur_assigned_to != '3050') {
                obj.splice(k, 1)
            }
        }
    }
}

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

相关问题 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 根据条件将文本从一个文本框复制到另一个文本框 - copy text from one textbox to another based on a condition 根据另一个数组 Object 作为条件过滤对象数组 - Filter the Array of objects based on another Array Object as condition 如何根据特定值将数组中的一个object复制到另一个数组中 - How to copy an object in the array to another array based on a specific value 根据条件将一个数组复制到另一个数组 - Copy an array to another array on condition 根据条件过滤 object - Filter object based on condition Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 如何将对象数组组合成一个 object,然后根据索引条件在该数组中创建另一个 object - How to combine array of objects into one object and then create another object within that array based on a condition on the index 更改对象基于结构的条件 - change object Structure based condition 根据if条件在Firebase中更新对象 - Updating object in Firebase based on a if condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM