Home > Back-end >  how to convert a items in a object to list of array
how to convert a items in a object to list of array

Time:06-17

{
    Type: "Fire",
    Name: "Shark",
    StartRange0: "10",
    EndRange0: "20",
    StartRange1: "30",
    EndRange1: "40",
    StartRange2: "60",
    EndRange2: "70"
}

In the above object, StartRange and EndRange can be more items in different objects. i.e those values are obtained from a loop( StartRange index), so there maybe StartRange1,2,3... lly, EndRange1,2,3...

The above object structure must be changed to,

{
   "Fire":{
      "Name":"Shark",
      "List":[
         [
            "10",
            "20"
         ],
         [
            "30",
            "40"
         ],
         [
            "60",
            "70"
         ]
      ]
   }
}

The List array inside the object is combination of [StartRange0,EndRange0],[StartRange1,EndRange2]... So on...

I tried,

let newObj = {}
newObj[oldObj.Type] = {
    Name: oldObj.Name, //oldObj is the first object defined above.
    List: [[oldObj.StartRange0,oldObj.EndRange0],[oldObj.tartRange1,oldObj.EndRange1],[oldObj.StartRange2,oldObj.EndRange2]]
}

But here, it is limited to StartRange2 and EndRange2 but I need it for StartRangeN and EndRangeN.

CodePudding user response:

This is how I would have done map it

const input = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70"
}

const toList = (l) => Array.from({
  length: Object.keys(l).length / 2
}, (_, N) => [l[`StartRange${N}`], l[`EndRange${N}`]])

const toOutput = ({ Type, Name, ...rest }) => {
  const List = toList(rest)
  return ({
    [Type]: {
      Name,
      List
    }
  })
}

const output = toOutput(input)
console.log(output)

CodePudding user response:

You can play around with this example

const object = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70"
};

let index = 0;
let isFinished = false;
const results = [];

while (!isFinished) {
  const startKey = `StartRange${index}`;
  const endKey = `EndRange${index}`;

  isFinished = !object.hasOwnProperty(startKey) || !object.hasOwnProperty(endKey);

  !isFinished && results.push([object[startKey], object[endKey]]);

  index  ;
}

console.log(results);

CodePudding user response:

This may work but it would be better if you could change that structure and use another one.

const test = {
  Type: "Fire",
  Name: "Shark",
  StartRange0: "10",
  EndRange0: "20",
  StartRange1: "30",
  EndRange1: "40",
  StartRange2: "60",
  EndRange2: "70",
};

const format = (obj) => {
  const { Type, Name, ...rest } = obj;
  const list = [];
  const keys = Object.keys(rest);
  for (let index = 0; index < keys.length; index  ) {
    const start = rest[`StartRange${index}`];
    const end = rest[`EndRange${index}`];
    if (start && end) {
      list.push([start, end]);
    } else {
      break; // assuming you don't have any other elements
    }
  }
  return {
    [Type]: {
      Name,
      List: list,
    },
  };
};

console.log(JSON.stringify(format(test)));
  • Related