Turn multidimensional array into one-dimensional VUE

Asked

Viewed 185 times

0

People I need to transform a multidimensional array into a one-dimensional array, because I need to recover an array only with the "Cod" fields of this array, tended as below without success.

new Vue({
    el:"#app",
    data : {
        array_teste : [{cod : '1', nome : 'S'},{cod : '2', nome : 'V'}],
    },

    computed: {
        cat_vender_rec: function() {
            return JSON.stringify(this.cat_vender);
        }
    }       
})
  • 2

    Just remembering that you don’t have a multi-dimensional array there... You have an array of objects with dimension 1.

1 answer

3

You can use the method .map() to map the array data you want.

const array = [{cod : '1', nome : 'S'},{cod : '2', nome : 'V'}];

const arrayCods = array.map(obj => {
  return obj.cod;    
});

console.log(arrayCods); // ['1', '2']

Documentation of the . map method()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.