Vishnu S Babu Vishnu S Babu - 7 months ago 10
Javascript Question

Convert a Comma Seperate Value to JSON Object in Javascript

I want to Convert a Comma Separated value of array to JSON object to formats below mentioned

INPUT

swedish,chinese,english,hindi

OUTPUT

Object {swedish: swedish, chinese: chinese, english: english,hindi: hindi}

Answer

This will do

var temp = {"data":"swedish,chinese"}
temp.data= temp.data.split(',').reduce((res,x)=>{
  res[x] = x;
  return res
},{});
console.log(temp);

will give

{"data":{"swedish":"swedish","chinese":"chinese"}}