-1

I want to convert the below object to the expected one. I am using the Javascript/node js code to do it, but could not able to achieve it. I have tried so many ways but did not achieve it.

Original:


{
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
}

Expected:

[
  {
    "market": "CORE",
    "buildVersion": [
      "6.15",
      "6.16"
    ]
  },
  {
    "market": "US",
    "buildVersion": [
      "6.15",
      "6.16"
    ]
  },
  {
    "market": "CA",
    "buildVersion": [
      "6.15.1",
      "6.16.2"
    ]
  }
]
Sandeep
  • 55
  • 7

4 Answers4

1

The easiest way to accomplish this is by calling Object.values on the array-like objects.

const data = {
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
};

const fixed = Object.values(data).map(({ buildVersion, ...rest }) =>
  ({ ...rest,  buildVersion: Object.values(buildVersion) }));

console.log(fixed);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Here is a more dynamic example that uses recursion. It checks if the object is array-like, before converting it.

const data = {
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
};

const fix = o =>
  typeof o === 'object'
    ? Object.keys(o).every(k => isFinite(k))
      ? Object.entries(o).map(([k, v]) => fix(v))
      : Object.fromEntries(Object.entries(o).map(([k, v]) => [k, fix(v)]))
    : o;

const fixed = fix(data);

console.log(fixed);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Just note that if the order of the output array must match the keys in the original object, this may or may not work if the object is manipulated in a way which changes the key order. – cbr Jul 09 '21 at 17:36
  • @cbr Object key order is always ordered numerically from 0 -> MAX_SAFE_INTEGER. The order does not matter when written. – Mr. Polywhirl Jul 09 '21 at 17:41
  • Oh really? Thanks! I didn't know that! – cbr Jul 09 '21 at 17:42
  • 1
    @cbr iteration of integer-like keys are always [ascending](https://stackoverflow.com/a/38218582/1762224). – Mr. Polywhirl Jul 09 '21 at 17:43
1

If we assume that the objects' keys always start from 0 and are contiguous (no missed numbers in between), you could just use Object.entries() (or Object.keys() and indexing) to grab the key and value and populate an array from them:

const obj = {
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
}

const numberedObjectToArray = (obj) => {
  // Assuming `obj` has contiguous index numbers starting from 0
  return Object.entries(obj).reduce((arr, [key, value]) => {
    const index = parseInt(key, 10);

    arr[index] = value;
    return arr;
  }, []);
};

const array = numberedObjectToArray(obj).map(({ buildVersion, ...o }) => ({
  ...o,
  buildVersion: numberedObjectToArray(buildVersion)
}));

console.log(array);
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
cbr
  • 12,563
  • 3
  • 38
  • 63
1

object = {
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
};

let output = [];

for (let okey in object) {
    let market = object[okey].market;
  let build = object[okey].buildVersion;
  let buildVersion = [];
  for (let bkey in build){
    buildVersion.push(build[bkey]);
  }
  
  output.push({
   market,
    buildVersion
  });
}

console.log(output);
Rui Costa
  • 417
  • 2
  • 11
1

var obj= {
  "0": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "CORE"
  },
  "1": {
    "buildVersion": {
      "0": "6.15",
      "1": "6.16"
    },
    "market": "US"
  },
  "2": {
    "buildVersion": {
      "0": "6.15.1",
      "1": "6.16.2"
    },
    "market": "CA"
  }
}
const output=[];
Object.values(obj).map(function(curr){
  const {buildVersion,market}= curr;
  output.push({
    market:market,
    buildVersion:Object.values(buildVersion)
  })
});
console.log(output);

`

ILAYA RAJA
  • 13
  • 3