Home > Back-end >  Javascript how to convert Json array values into string using join
Javascript how to convert Json array values into string using join

Time:08-24

I have a json string as output from a function.

{"0":{"error":"ERROR_SPECIAL_CHAR","message":"Missing a special character"},"1":{"error":"ERROR_CHAR_UPPERCASE","message":"Must contain at least one uppercase letter"}

I want to get the output in the format. "Missing a special character, Must contain at least one uppercase letter"

I tried Array.isArray(err)? err.join(','), but I get the output [object Object],[object Object]

CodePudding user response:

basically access the individual properties then join them. However since there might be a few of them, we need a little loop.

var obj = {
  "0": {
    "error": "ERROR_SPECIAL_CHAR",
    "message": "Missing a special character"
  },
  "1": {
    "error": "ERROR_CHAR_UPPERCASE",
    "message": "Must contain at least one uppercase letter"
  }
}

var messages = []
for (var key in obj) {
  messages.push(obj[key].message)
}
var str = messages.join(", ");
console.log(str)

  • Related