I have a data object in Vue JS called tags that looks like this:
tags: [
{name: "menu"},
{name: "red"}
],
Currently I am able to output them with this
var tags = this.tags;
var tagss = JSON.stringify(tags);
console.log('list of tags:' + tagss);
but it returns it like this:
list of tags:[{"name":"menu"},{"name":"red"}]
and I want it to return them like this:
list of tags: menu,red
Any idea how to do this? The reason I want it like this is so I can query my API with a list of tags.
Thanks in advance.
You can use Array.map
to iterate over the array and extract the names to a new array, then join the items to create the comma separated string, like so:
tags.map(({ name }) => name).join(', ');
all you need is just convert js array of objects to a string,
so first, we will convert the array of objects to indexed array, then convert array to string
after this line ->var tags = this.tags;
var tagss = tags.map(function(tag) {
return tag['name'];
});
console.log(tagss); //output should be [menu,red]
tagss_string = tagss.toString();
console.log(tagss_string); //output should be menu,red
var tags_string = '';
for (var i=0;i<tags.length;i++){
if (i!=0)
tags_string += ','+tags[i].name ;
else
tags_string += tags[i].name ;
}
console.log(tags_string); //output should be menu,red
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With