Alejandra Jaramillo
Alejandra Jaramillo

Reputation: 35

how can I transform JSON file to Array on VUEjs?

how it's possible to change the object like this:

data() .... emojiList: [];

methods: {
  //Generated by JSON file
  //{"sweat_smile":"😅","laughing":"😆","satisfied":"😆","innocent":"😇","smiling_imp":"😈","wink":"😉"};

  var emoji = emoji.emoji;

  this.emojiList = emoji;
}

but when I render only get the value, I'd like to make a new array with the previews, where I can set

{"name":"smile", "icon":"😅"}

can anybody understand me?

Upvotes: 0

Views: 195

Answers (1)

balexandre
balexandre

Reputation: 75073

if your input is

{"sweat_smile":"😅","laughing":"😆","satisfied":"😆","innocent":"😇","smiling_imp":"😈","wink":"😉"}

then you can simply do:

var input = { "sweat_smile":"😅", "laughing":"😆", "satisfied":"😆", "innocent":"😇", "smiling_imp":"😈", "wink":"😉" }

var output = []

Object
  .keys(input)
  .forEach(k => { 
     output.push({ name: k, icon: input[k] })
  })

console.log(JSON.stringify(output, null, 4))

Upvotes: 2

Related Questions