Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm facing some problems with objects, json and etc in js. If some one can help me with something, it would be amazing! :)

I have an object, that I can't change, like this:

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

I want to convert to be like this:

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

So I want the object values (arrays) to be string, I also tried toString(), String(), JSON.stringify, and other approaches from the internet, (maybe I had not done it right) but none worked for me, I'm kind of new on dealing with these type of data, so if some one can help me, TKS!! :D

EDIT:

And in the case I got this structure:

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

How can I convert to be like:

{
  "page": "POST, PUT:122",
  "news": "PUT"
}
share|improve this question
1  
so loop over the object and use join – epascarello Jul 25 at 20:45
    
you've said an object, that I can't change, because the initial object should remain immutable ? – RomanPerekhrest Jul 25 at 20:52
up vote 0 down vote accepted

If you want to modify the initial object - it would be enough to use Object.keys and Array.forEach functions:

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

Object.keys(obj).forEach(function(k) {
  obj[k] = obj[k].join(",");
});

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

Additional solution for another complex case:

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));

share|improve this answer
    
Tks RomanPerekhrest, yours and @epascarello answers are the best ones. Actually, I can change the inicial structure at the save moment. – Lucas Santos Jul 26 at 21:25
    
I made an edit @RomanPerekhrest, if u can help me with this second case it would be nice. Tks in advance! :) – Lucas Santos Jul 26 at 21:35
    
@LucasSantos, done! See my update – RomanPerekhrest Jul 27 at 4:39
    
Thank you so much @RomanPerekhrest, amazing implementation! I learned too much here. Tks – Lucas Santos Jul 27 at 13:25
    
@LucasSantos, you're welcome – RomanPerekhrest Jul 27 at 13:33

With a assumption that your object will always have arrays as property values, you can do this using javascript like below

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


 for (var o in obj) {
     if (obj.hasOwnProperty(o)) {
         obj[o] = obj[o].join(", ");
     }
 }

 console.log(obj);
share|improve this answer
    
This works, but I think the Object.keys approach is better. Tks anyway @HectorBarbossa – Lucas Santos Jul 26 at 14:37

Ok, so you can add all values of an array up (concatenate them) to a string by using var str = array.join(stringThatIsSupposedToBeInbetweenTheValues);. So in your case that would be array.join(', ');. Note that this string is only put inbetween the single values, not around them (at the beginning or the end of the result). More info here.

I hope that helps!

share|improve this answer
    
A good reference, I really got it. Tks – Lucas Santos Jul 26 at 14:38

Plenty of ways to do it. Just need to loop over the object and set the array to a string.

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

Object.keys(obj).reduce( function (o, key) {  
    o[key] = o[key].join(", ");
    return o;
}, obj);

console.log(obj);

Probably would make sense to use forEach, but reduce works.

share|improve this answer
    
Tks epascarello, yours and @RomanPerekhrest answers are the best ones. I opt for the forEach approach. – Lucas Santos Jul 26 at 14:55

You may do like this;

var o = {
  "page": [
    "POST",
    "DELETE"
  ],
  "news": [
    "PUT"
  ]
},
    p = Object.keys(o).reduce((p,k) => (p[k] = o[k]+"",p),{});
console.log(p);

share|improve this answer
    
Thats looks great, but I got an error on Webstorm and JSHint testing this code. But looks just like @epascarello answer. – Lucas Santos Jul 26 at 14:44
1  
@LucasSantos That would probably be because you have ES6 disabled and that fat arrow function (p,k) => (p[k] = o[k]+"",p),{}) doesn't work before ES6. See MDN. To enable ES6 in WebStorm open Setting --> Lanuages & Frameworks --> JavaScript and choose ECMAScript 6 instead of 5.1 – LBBO Jul 26 at 14:54
    
Nice @LBBO it's active now, I'm studying the ES6, got a book yesterday. :D – Lucas Santos Jul 27 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.