0

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",
        }
      }
    }
  }
}
8
  • 2
    Your converted format is not valid JavaScript. Please update it. Commented May 26, 2020 at 19:37
  • @terrymorse I'm not sure on that, I got this format from the backend side and backend supporting that, could you please let me know, anyhow is it possible to achieve that JSON Object. Thanks Commented May 27, 2020 at 4:54
  • I’m sure. Missing comma, semicolons where there should be commas. Commented May 27, 2020 at 5:19
  • ooh, that is I think my mistake it should be Commented May 27, 2020 at 5:41
  • 1
    How do you decide AND and OR of action key ? Commented May 27, 2020 at 16:01

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.