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

时间:2020-09-25 04:54:05

标签: javascript jquery

我正在尝试transform一个对象,或者换句话说,我正在尝试更改一个对象的文本。

我有two object个是源,另一个是mapped object。我想用mapped object来转换源对象。

示例

source object

{
  "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/"
          }   
        ]
      }
  }
}

mapped -object

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

expectedout 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/"
          }   
        ]
      }
  }
}

我在{{1}中有id'ssource obj中有相同的id's。为此,我想更改所有mapped obj的文本。

我尝试过,这是我的整个代码

https://jsbin.com/zaxoyineme/edit?js,console,output

properties and keys

我正在利用经济衰退来获得预期的产出,但我无法获得预期的产出

1 个答案:

答案 0 :(得分:0)

您的代码会使您要执行的操作变得复杂。据我了解,这应该可以完成您的工作。

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)