How to parse an objects with array index to object array with key value pairs

Shankar Kumar
{"SubmitBy":"SK", "Students[0].name":"Jhon", "Students[0].age":"15", "Students[1].name":"Sam", "Students[1].age":"16", "Students[2].name":"Tom", "Students[2].age":15} 

Result expecting like this

{SubmitBy:"SK", Students:[{name:"Jhon", age:"15"},{name:"Sam", age:"16"},{name:"Tom", age:"15"} ]}
debug_dk

To parse an object with array index to an object array with key-value pairs in JavaScript, you can use the following function:

function parseObject(input) {
  const result = {};
  Object.keys(input).forEach((key) => {
    const index = key.indexOf("[");
    if (index > -1) {
      const root = key.substring(0, index);
      const subKey = key.substring(index + 1, key.length)
      const indexNum = parseInt(subKey, 10);
      if (!result[root]) {
        result[root] = [];
      }
      if (!result[root][indexNum]) {
        result[root][indexNum] = {};
      }
      result[root][indexNum][subKey.substring(subKey.indexOf(".") + 1)] = input[key];
    } else {
      result[key] = input[key];
    }
  });
  return result;
}
const input = {"SubmitBy":"SK", "Students[0].name":"Jhon", "Students[0].age":"15", "Students[1].name":"Sam", "Students[1].age":"16", "Students[2].name":"Tom", "Students[2].age":15};
const output = parseObject(input);
console.log(output);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

How to create an object from an Array of key-value pairs?

Array of key/value pairs to object

How to convert an array of objects to object with key value pairs

Converting an array into an object with key/value pairs

How to push object literals as key-value pairs to array?

Transform an array of key/value pairs into array of objects

how to split a js object into an array of key-value pairs?

how to convert array of objects to key value pairs in php

how to find a value in array of objects with key value pairs

How do I turn key value pairs in object into array of objects?

How to push an object with two key/value pairs into an array?

How to recursively add object key-value pairs to an array of objects with child arrays?

Convert array of objects to object of key-value pairs

Javascript: How to make array from object with key value pairs as strings?

convert an array and an array of arrays into array of objects with key value pairs

Build an array of objects with two key/value pairs

How to convert object containing key-value pairs to an array of objects in JavaScript

How to convert an array into an object in javascript with mapped key-value pairs?

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to specify array of key value pairs for a generic object in Typescript?

Key value pairs array into array object

Transform key/value pairs object into array of objects using Vanilla JS

How to convert array to an object with key/value pairs Javascript

How to split an array of strings & return a single object with key/value pairs

Filter for array of objects that contain all key value pairs of one object in another array of objects

How to get certain key value pairs from an array of object

Map array of objects to object of key value pairs