angularjs - javascript - Convert object values from string to array structure

Lucas Santos

I'm studying some complex cases on json convert with js, and I have a situation like this:

I got this:

{
  "page": "POST,PUT:122",
  "news": "PUT"
}

And I want to convert to this:

{
  "page": {
    "POST": [
      "POST"
    ],
    "PUT": 122
  },
  "news": {
    "PUT": [
      "PUT"
    ]
  }
}

I'm already doing this conversion on a revert case with this great solution by @RomanPerekhrest, I want to get back the same structure, but I tried a few ways with no success, Roman's solution:

var obj = {
  "page": {
    "POST": [
      "POST"
    ],
    "PUT": 122
  },
  "news": {
    "PUT": [
      "PUT"
    ]
  }
};

Object.keys(obj).forEach(function (k) {
  var innerKeys = Object.keys(obj[k]), items = [];
  innerKeys.forEach(function(key) {
    items.push((Array.isArray(obj[k][key]))? key : key + ":" + obj[k][key]);
  });
  obj[k] = items.join(",");
});

console.log(JSON.stringify(obj, 0, 4));

Vladu Ionut

var obj = {
  "page": "POST,PUT:122",
  "news": "PUT"
};
var rez={};
Object.keys(obj).forEach(function(key){
 obj[key].split(",").forEach(function(value){
   var item = value.split(":");
   var obj=rez[key] || {};
   if (item.length>1){     
     obj[item[0]]=isNaN(Number(item[1])) ? item[1] : Number(item[1]);
     rez[key]=obj;
   }else{
      obj[item[0]]=[item[0]];
      rez[key]=obj;
   }
 });
});
console.log(rez);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angularjs - javascript - Convert object values from array to string

From Dev

Convert array of object to single object in javascript(Angularjs)

From Dev

Convert string loaded from text file to an array object in JavaScript

From Dev

Convert string Array to Object Array in javascript

From Dev

Convert javascript array to object of same keys/values

From Dev

Join an array object and string variable in javascript or angularjs

From Dev

Add values from Object in Array Object in Javascript

From Dev

Javascript Convert String to Object(list/array)

From Dev

Convert Object to Array angularjs

From Dev

convert array into an object angularjs

From Dev

Select values of a column in an object json then convert it to array AngularJs

From Dev

Convert Json object array to list of property values in angularjs

From Dev

How to get specific object values from java <List> and convert to String Array

From Dev

How to convert an object from PHP to an Array in Javascript?

From Java

Convert any null or falsy values in an array to blank string "" values Javascript

From Dev

AngularJS Convert String to Object/Array that references the objects in the $scope

From Dev

Insert an array structure in the string in AngularJS

From Dev

Javascript replace string with values from dom object

From Dev

Creating javascript object from array values

From Dev

Sending values from an array of object to a function in JavaScript

From Dev

Transfer Values from Object to an Array - Javascript

From Dev

How to convert Associative array to a simple string array of values in javascript

From Dev

How do I convert string array values to object in c#

From Dev

javascript: push array values from string variable

From Dev

Convert array into different structure using array values

From Dev

How to parse a string from servlet to javascript and created a formatted object which contains array values

From Dev

Javascript: Convert string to object

From Java

Convert Array into Object of Keys and Values using javascript/jquery

From Dev

convert array to object javascript

Related Related

  1. 1

    angularjs - javascript - Convert object values from array to string

  2. 2

    Convert array of object to single object in javascript(Angularjs)

  3. 3

    Convert string loaded from text file to an array object in JavaScript

  4. 4

    Convert string Array to Object Array in javascript

  5. 5

    Convert javascript array to object of same keys/values

  6. 6

    Join an array object and string variable in javascript or angularjs

  7. 7

    Add values from Object in Array Object in Javascript

  8. 8

    Javascript Convert String to Object(list/array)

  9. 9

    Convert Object to Array angularjs

  10. 10

    convert array into an object angularjs

  11. 11

    Select values of a column in an object json then convert it to array AngularJs

  12. 12

    Convert Json object array to list of property values in angularjs

  13. 13

    How to get specific object values from java <List> and convert to String Array

  14. 14

    How to convert an object from PHP to an Array in Javascript?

  15. 15

    Convert any null or falsy values in an array to blank string "" values Javascript

  16. 16

    AngularJS Convert String to Object/Array that references the objects in the $scope

  17. 17

    Insert an array structure in the string in AngularJS

  18. 18

    Javascript replace string with values from dom object

  19. 19

    Creating javascript object from array values

  20. 20

    Sending values from an array of object to a function in JavaScript

  21. 21

    Transfer Values from Object to an Array - Javascript

  22. 22

    How to convert Associative array to a simple string array of values in javascript

  23. 23

    How do I convert string array values to object in c#

  24. 24

    javascript: push array values from string variable

  25. 25

    Convert array into different structure using array values

  26. 26

    How to parse a string from servlet to javascript and created a formatted object which contains array values

  27. 27

    Javascript: Convert string to object

  28. 28

    Convert Array into Object of Keys and Values using javascript/jquery

  29. 29

    convert array to object javascript

HotTag

Archive