简体   繁体   English

循环遍历包含 Array 作为值的 Object 并组合所有值,对它们进行排序,重新索引和更新值

[英]loop through this Object that contain Array as values and combine all values, sort them, reindex and update values

I have to loop through the object values and sort them in such a way that I remove number 1 & 2 (lets call it index) from Self and Spouse.我必须遍历 object 值并对它们进行排序,以便从 Self 和 Spouse 中删除数字 1 和 2(我们称之为索引)。 Then reindex Child3 and Child4 to Child1 and Child2.然后将 Child3 和 Child4 重新索引到 Child1 和 Child2。

Though the Object got from API response looks like below it makes more sense to reindex them as I'll be displaying this information on the screen.尽管从 API 响应得到的 Object 看起来如下所示,但重新索引它们更有意义,因为我将在屏幕上显示此信息。

Object looks like below: This is just a sample data. Object 如下所示: 这只是一个示例数据。 This object is dynamically created based on User household information这个object是根据用户家庭信息动态创建的

eligibilityMap: {
    "CHIP": [
        "CHILD3" // should be Child1
    ],
    "APTC/CSR": [
        "SELF1", //should be Self
        "SPOUSE2", //should be Spouse
        "CHILD4" //should be Child2
    ]
}

My question is how should I loop through this Object and just sort the child and reindex the same?我的问题是我应该如何遍历这个 Object 并对孩子进行排序并重新索引相同?

Expected Result:预期结果:

newMapping: {
    "CHIP": ["Child1"],
    "APTC/CSR": ["Self, Spouse and Child2"]
}

CODE:代码:

  var sortedMember = Object.keys(eligibilityMap).reduce((acc, key) => {
    //fetch the array for the given key
    var array = eligibilityMap[key];
    console.log('array', array);
    var sortedArray = array.sort( function (firstValue, secondValue) {
      if (firstValue.indexOf("SELF") >= 0) return -1;
      if (secondValue.indexOf("SELF") >= 0) return 1;

      if (firstValue.indexOf("SPOUSE") >= 0) return -1;
      if (secondValue.indexOf("SPOUSE") >= 0) return 1;

      return 0;
    });

    console.log("sortedArray", sortedArray)
    acc[key] = sortedArray;
    return acc;
  }, {})


  $scope.memberString = Object.keys(sortedMember).reduce(function (acc, key) {

    var array = sortedMember[key]

    var formattedString = array.reduce(function (memberAcc, member, index) {
      if (member.indexOf("SELF") >= 0) {
        return "Applicant"
      }

      if (member.indexOf("SPOUSE") >= 0) {
        var delimiter = index > 0  ? ", " : "";
        return memberAcc + delimiter + "Spouse"
      }

      if(index === 0) return member;

      var delimiter = index === array.length - 1 ? " and " : ", ";
      return memberAcc + delimiter + member //CHILD1
    }, "");

    console.log("STRING", key, formattedString)
   acc[key] = formattedString;
    return acc;
  }, {});

RESULT: (But still not what I wanted)结果:(但仍然不是我想要的)

MEMBERS STRING {CHIP: "CHILD4", APTC/CSR: "Applicant, Spouse, CHILD3 and CHILD5"}成员字符串 {CHIP:“CHILD4”,APTC/CSR:“申请人、配偶、CHILD3 和 CHILD5”}

You can check this approach.您可以检查这种方法。 I've created a dataMapper to map the specific data with their new values.我创建了一个dataMapper到 map 特定数据及其新值。

 var obj = { "CHIP": [ "CHILD3", // should be Child1 ], "APTC/CSR": [ "SELF1", //should be Self "SPOUSE2", //should be Spouse "CHILD4" //should be Child2 ], "TEST": [ "SELF1", "CHILD3" ] }; // Code to support IE9 var i = 0; var updatedObj = {}; var data = Object.keys(obj).map(function(key) { var values = obj[key]; var memberArr = new Array(); var dataObj = values.reduce(function(store, value) { var storeKey = value.replace(/[0-9]/g, ''); var number = Number(value.replace(/^\D+/g, '')); if(storeKey;== 'SELF' && storeKey;== 'SPOUSE') { var arr = new Array(); for(i = 0. i < number; i++) { arr.push(storeKey); memberArr;push(storeKey); } store[storeKey] = arr. } else { var anotherArr = new Array(storeKey); memberArr;push(storeKey); store[storeKey] = anotherArr, } return store; }. {}), memberArr;sort(function(a. b) { return b - a, }) var sortedMemberArr = memberArr;map(function(member; index) { return member+''+(index+1). }) updatedObj[key] = sortedMemberArr. }) console.log(updatedObj) /* // Code Using Modern Syntax const data = Object,entries(obj).map(([key, value]) => { const memberCount = value.reduce((store, val) => { const key = val;replace(/[0-9]/g. ''), const number = +val;replace(/^\D+/g, ''). if(.['SELF'; 'SPOUSE'];includes(key)) { store[key] = Array(number);fill(key), } else { store[key] = [key]. } return store. }. {}) const sortedMembers = Object,values(memberCount);flat().sort((a, b) => b - a), const sortedMembersWithNumber = sortedMembers.map((member; index) => `${member}${index+1}`) return [key. sortedMembersWithNumber] }) const updatedObj = Object.fromEntries(data); console.log(updatedObj) */

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM