Home > Mobile >  Convert Javascript object to a particular format
Convert Javascript object to a particular format

Time:09-28

I have an object of this format

[
    {
      "major_segment": "A",
      "minor_segments": [
        {
          "segment_names": [
            "A11",
            "A12",
            "A13",
            "A14"
          ],
          "minor_segment_name": "List1"
        },
        {
          "segment_names": [
            "A21",
            "A22",
            "A23",
            "A24"
          ],
          "minor_segment_name": "List2"
        },
    {
      "major_segment": "B",
      "minor_segments": [
        {
          "segment_names": [
            "B11",
            "B12",
            "B13"
          ],
          "minor_segment_name": "List3"
        },
        {
          "segment_names": [
            "B21",
            "B22",
            "B23"
          ],
          "minor_segment_name": "List4"
        }
  ]

I want to convert this into another format like:

myObj =  [
    {
      "major_segment": "A",
      "minor_segments": [
        {
          "segment_names": [
            { "name":"A11", id:1},
            { "name":"A12", id:2},
            { "name":"A13", id:3},
            { "name":"A14", id:4}
          ],
          "minor_segment_name": "List1"
        },
        {
          "segment_names": [
            { "name":"A21",id:5},
            { "name":"A22",id:6},
            { "name":"A23",id:7},
            { "name":"A24",id:8},
          ],
          "minor_segment_name": "List2"
        },
    {
      "major_segment": "B",
      "minor_segments": [
        {
          "segment_names": [
            { "name":"B11",id:9},
            { "name":"B12",id:10},
            { "name":"B13",id:11}
          ],
          "minor_segment_name": "List3"
        },
        {
          "segment_names": [
            { "name":"B21",id:12},
            { "name":"B22",id:13},
            { "name":"B23",id:14}
          ],
          "minor_segment_name": "List4"
        }
  ]

Can anyone help?

CodePudding user response:

So in other words you want to add id`s to the minor segment entries? I loop the object recursively, and if I find a string value that's inside an array, I transform it to the required format.

var arr = [{major_segment:"A",minor_segments:[{segment_names:["A11","A12","A13","A14"],minor_segment_name:"List1"},{segment_names:["A21","A22","A23","A24"],minor_segment_name:"List2"}]}];


function iterate(obj) {
  Object.keys(obj).forEach(function(key) {
    var value = obj[key];
    if (typeof value === 'object' && value != null) {
      iterate(value)
    } else {
      if (typeof value === "string" && Array.isArray(obj)) {
        obj[key] = {name: value, id:   id}
      }
    
    }
  })
}
var id=0;

arr.forEach(iterate);
console.log(arr)
.as-console-wrapper {max-height: 100% !important}

CodePudding user response:

Your original input is missing several ], }. Check it.

You would have to use Array#map many times and also apply some object destructuring and aliasing as follows:

const input = [ { "major_segment": "A", "minor_segments": [ { "segment_names": [ "A11", "A12", "A13", "A14" ], "minor_segment_name": "List1" }, { "segment_names": [ "A21", "A22", "A23", "A24" ], "minor_segment_name": "List2" }] }, { "major_segment": "B", "minor_segments": [ { "segment_names": [ "B11", "B12", "B13" ], "minor_segment_name": "List3" }, { "segment_names": [ "B21", "B22", "B23" ], "minor_segment_name": "List4" }] } ];
  
let id = 0;
  
const output = input.map(
    ({minor_segments:ms,...rest}) =>
    ({...rest,minor_segments:ms.map(
      ({segment_names:sn,...r}) => 
      ({segment_names:sn.map(name => ({name,id:  id})),...r})
    )})
  );
  
  console.log( output );

  • Related