Javascript 如何将源对象转换为另一个对象?

Javascript 如何将源对象转换为另一个对象?,javascript,jquery,Javascript,Jquery,我正在尝试变换对象,或者换句话说,我正在尝试改变对象的文本 我有两个对象,一个是源对象,另一个是映射对象。使用映射对象,我想变换源对象 范例 源对象 { "links": { "Offering": { "id":"offering-id", "iconUrl": "", "links": [

我正在尝试变换对象,或者换句话说,我正在尝试改变对象的文本

我有两个对象,一个是源对象,另一个是映射对象。使用映射对象,我想变换源对象

范例

源对象

{
  "links": {
    "Offering": {
      "id":"offering-id",
      "iconUrl": "",
      "links": [
        {
          "text": "School",
          "id":"school-id",
          "onclickUrl": "https://www.school.com/school/"
        },
        {
          "text": "happy",
          "onclickUrl": "https://www.happy.com/help/",
          "id":"happy-id"
        }    
      ]
    },
    "luck": {
        "iconUrl": "",
       "id":"hospital-id",
        "links": [
          {
            "text": "Student",
            "id":"student-id",
            "onclickUrl": "https://www.student.com/school/"
          }   
        ]
      }
  }
}
let mappingObj = {
  "hospital-id":"hospital-change",
  "school-id":"school-change-xx",
  "offering-id":"Offering-changes-map",
  "happy-id":"happy-cs",
  "student-id":"nnnnnn"
}
映射-对象

{
  "links": {
    "Offering": {
      "id":"offering-id",
      "iconUrl": "",
      "links": [
        {
          "text": "School",
          "id":"school-id",
          "onclickUrl": "https://www.school.com/school/"
        },
        {
          "text": "happy",
          "onclickUrl": "https://www.happy.com/help/",
          "id":"happy-id"
        }    
      ]
    },
    "luck": {
        "iconUrl": "",
       "id":"hospital-id",
        "links": [
          {
            "text": "Student",
            "id":"student-id",
            "onclickUrl": "https://www.student.com/school/"
          }   
        ]
      }
  }
}
let mappingObj = {
  "hospital-id":"hospital-change",
  "school-id":"school-change-xx",
  "offering-id":"Offering-changes-map",
  "happy-id":"happy-cs",
  "student-id":"nnnnnn"
}
预期输出

let expectedOutput = {
  "links": {
    "Offering-changes-map": {
      "id":"offering-id",
      "iconUrl": "",
      "links": [
        {
          "text": "school-change-xx",
          "id":"school-id",
          "onclickUrl": "https://www.school.com/school/"
        },
        {
          "text": "happy-cs",
          "onclickUrl": "https://www.happy.com/help/",
          "id":"happy-id"
        }    
      ]
    },
    "hospital-change": {
        "iconUrl": "",
       "id":"hospital-id",
        "links": [
          {
            "text": "nnnnnn",
            "id":"student-id",
            "onclickUrl": "https://www.student.com/school/"
          }   
        ]
      }
  }
}
我在
源obj中有
id
相同的
id
存在于
映射obj
中。我想用它来更改所有
属性和键的文本

我试过了这是我的全部密码

let result={};
函数递归fn(Obj){
用于(让我输入Obj){
结果[mappingObj[Obj[i].id]| | i]=Obj[i];
if(对象类型[i]=“对象”){
log('--obj--');
递归fn(Obj[i]);
}else if(Object.prototype.toString.call(Obj[i])=='[Object Array]'){
log('--Array--');

对于(var k=0;k来说,您的代码对于您正在尝试做的事情来说非常复杂。从我的理解来看,这应该可以完成您的工作

let Obj = {
  "links": {
    "Offering": {
      "id":"offering-id",
      "iconUrl": "",
      "links": [
        {
          "text": "School",
          "id":"school-id",
          "onclickUrl": "https://www.school.com/school/"
        },
        {
          "text": "happy",
          "onclickUrl": "https://www.happy.com/help/",
          "id":"happy-id"
        }    
      ]
    },
    "luck": {
        "iconUrl": "",
       "id":"hospital-id",
        "links": [
          {
            "text": "Student",
            "id":"student-id",
            "onclickUrl": "https://www.student.com/school/"
          }   
        ]
      }
  }
}


let mappingObj = {
  "hospital-id":"hospital-change",
  "school-id":"school-change-xx",
  "offering-id":"Offering-changes-map",
  "happy-id":"happy-cs",
  "student-id":"nnnnnn"
}


// expected output

let expectedOutput = {
  "links": {
    "Offering-changes-map": {
      "id":"offering-id",
      "iconUrl": "",
      "links": [
        {
          "text": "school-change-xx",
          "id":"school-id",
          "onclickUrl": "https://www.school.com/school/"
        },
        {
          "text": "happy-cs",
          "onclickUrl": "https://www.happy.com/help/",
          "id":"happy-id"
        }    
      ]
    },
    "hospital-change": {
        "iconUrl": "",
       "id":"hospital-id",
        "links": [
          {
            "text": "nnnnnn",
            "id":"student-id",
            "onclickUrl": "https://www.student.com/school/"
          }   
        ]
      }
  }
}

let result = {};
let midResult = {};

function updateObject (Obj) {
  let i = 0;
  let parseArray = Object.keys(Obj.links);
  for (i =0; i < parseArray.length; i++) {
    let id = parseArray[i]
    let matchingId = mappingObj[Obj.links[id].id]
    midResult[matchingId] = Object.assign({}, Obj.links[id])
  }
}

updateObject(Obj);
result.links = Object.assign({}, midResult)



console.log(result)
让Obj={
“链接”:{
“提供”:{
“id”:“产品id”,
“iconUrl”:“,
“链接”:[
{
“文本”:“学校”,
“id”:“学校id”,
“onclickUrl”:https://www.school.com/school/"
},
{
“文本”:“快乐”,
“onclickUrl”:https://www.happy.com/help/",
“id”:“快乐id”
}    
]
},
“运气”:{
“iconUrl”:“,
“id”:“医院id”,
“链接”:[
{
“文本”:“学生”,
“id”:“学生id”,
“onclickUrl”:https://www.student.com/school/"
}   
]
}
}
}
设mappingObj={
“医院id”:“医院变更”,
“学校id”:“学校变更xx”,
“产品id”:“产品更改地图”,
“快乐id”:“快乐cs”,
“学生id”:“nnnnnn”
}
//预期产量
设期望输出={
“链接”:{
“提供更改地图”:{
“id”:“产品id”,
“iconUrl”:“,
“链接”:[
{
“文本”:“学校变更xx”,
“id”:“学校id”,
“onclickUrl”:https://www.school.com/school/"
},
{
“文本”:“快乐cs”,
“onclickUrl”:https://www.happy.com/help/",
“id”:“快乐id”
}    
]
},
“医院变革”:{
“iconUrl”:“,
“id”:“医院id”,
“链接”:[
{
“文本”:“nnnnnn”,
“id”:“学生id”,
“onclickUrl”:https://www.student.com/school/"
}   
]
}
}
}
让结果={};
设midResult={};
函数更新对象(Obj){
设i=0;
让parseArray=Object.keys(Obj.links);
对于(i=0;i