简体   繁体   中英

How to convert a JavaScript array to an object with a specified structure?

I have an array like the example logicData below.

I want to pass this array to a function that returns an object like the returnObject below.

In addition, using the returned object, I need to re-create the original logicData array.

Please anyone help me out. Thanks in Advance.

// array
var logicData = [
  {
    details: {
      name: "User-1",
      id: 1,
      age: "38",
    },
    company: "XYX",
    position: "Accountant",
  },
  {
    details: {
      name: "User-2",
      id: 2,
      age: "55",
    },
    company: "XYX",
    position: "Sales executive",
  },
  {
    details: {
      name: "User-3",
      id: 3,
      age: "25",
    },
    company: "XYX",
    position: "Engineer",
  },
  {
    details: {
      name: "User-4",
      id: 4,
      age: "29",
    },
    company: "XYX",
    position: "Engineer",
  },
  {
    details: {
      name: "User-5",
      id: 5,
      age: "32",
    },
    company: "XYX",
    position: "Sales executive",
  }
]

How to convert the array to below format

// require object formate
const returnObject = {
  first: {
    details: {
      name: "User-1",
      id: "1",
      age: "38",
    },
    company: "XYX",
    position: "Accountant",
  },
  second: {
    first: {
      details: {
        name: "User-2",
        id: 2,
        age: "55",
      },
      company: "XYX",
      position: "Sales executive",
    },
    second: {
      first: {
        details: {
          name: "User-3",
          id: 3,
          age: "25",
        },
        company: "XYX",
        position: "Engineer",
      },
      second: {
        first: {
          details: {
            name: "User-4",
            id: 4,
            age: "29",
          },
          company: "XYX",
          position: "Engineer",
        },
        second: {
          details: {
            name: "User-5",
            id: 5,
            age: "32",
          },
          company: "XYX",
          position: "Sales executive",
        }
      }
    }
  }
}

for converting structures like logicData to returnObject , you can use/reuse the following function:

function convertArrToObj(arr){
    let res = arr[arr.length - 1];
    for(let i = arr.length - 1; i > 0; i--){
        res = {
            first: arr[i - 1],
            second: res,
        }
    }
    return res;
}

convertArrToObj(logicData);

and for reverting the structures like returnObject back to logicData array structure, you can use/reuse this function:

function revertObjToArr(obj){
    const res = [];
    let temp = obj;
    while("second" in temp){
        res.push(temp.first);
        temp = temp.second;
    }
    res.push(temp);
    return res;
}

revertObjToArr(returnObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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