Is it possible to separate the value of the JSON item?

0

Let's say I have an object like this:

{
  "parametros": {
    "carteira": "RG", 
    "taxaboleto": "0.00", 
    "multa": "0.00", 
    "juros": "0.00"
  }
}

Is it possible that I can extract the item's name from the object? For example, the item "wallet": "RG", if I print on the screen as {{parametros.carteira}} (in Vue) I can print the value, but what if I need the name 'carteira' to be printed? How do I extract it?

Take into account that I do not know which object is coming, it might be that the items that are appearing are dynamic ...

    
asked by anonymous 05.06.2018 / 04:55

1 answer

0

You can use Object.keys() , where it returns the object's keys, instead of the values. I made a very simple example below:

HelloWorld.vue

<template>
  <div class="stackoverflow">
    <div v-for="(parametro, key) in parametrosKeys" :key="key">{{ parametro }}</div>
  </div>
</template>

<script>
import { parametros } from './parametros'

export default {
  name: 'Stackoverflow',
  data: () => ({
    parametros
  }),
  computed: {
    parametrosKeys () {
      return Object.keys(this.parametros)
    }
  }
}
</script>

parametros.json

{
  "parametros": {
    "carteira": "RG",
    "taxaboleto": "0.00",
    "multa": "0.00",
    "juros": "0.00"
  }
}

Note that in the example I make a computed that only returns the keys using Object.keys , but this could be done directly. The above example would return all keys like this:

    
05.06.2018 / 12:28