1

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"
}
2
  • 1
    so loop over the object and use join Commented Jul 25, 2016 at 20:45
  • you've said an object, that I can't change, because the initial object should remain immutable ? Commented Jul 25, 2016 at 20:52

5 Answers 5

0

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

Sign up to request clarification or add additional context in comments.

6 Comments

Tks RomanPerekhrest, yours and @epascarello answers are the best ones. Actually, I can change the inicial structure at the save moment.
I made an edit @RomanPerekhrest, if u can help me with this second case it would be nice. Tks in advance! :)
Thank you so much @RomanPerekhrest, amazing implementation! I learned too much here. Tks
Hi @RomanPerekhrest, I'm studying here and i'm trying to do a reverse case on the complex one, trying to get the same structure back, with no success, if you can and if you have a time could help me? Thanks.
@LucasSantos, it's a good opportunity to create a new question with new problem description. After you created it, let me know
|
0

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

1 Comment

This works, but I think the Object.keys approach is better. Tks anyway @HectorBarbossa
0

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!

Comments

0

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.

1 Comment

Tks epascarello, yours and @RomanPerekhrest answers are the best ones. I opt for the forEach approach.
0

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

3 Comments

Thats looks great, but I got an error on Webstorm and JSHint testing this code. But looks just like @epascarello answer.
@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
Nice @LBBO it's active now, I'm studying the ES6, got a book yesterday. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.