2

How to get each tag of tags property?

let array = [
 {name: "A", tags: ["a", "b"]}, 
 {name: "B", tags: ["a", "b", "c"]}
]
array.forEach(arr => {
 arr.tags.forEach(tag => {
 let str = `${arr.name} : ${tag}`;
 console.log(str);
 // result:
 // A : a
 // A : b
 // B : a
 // B : b
 // B : c
 })
})

But I want something like this

 // result:
 // A : <span>a</span> <span>b</span>
 // B : <span>a</span> <span>a</span> <span>c</span>

Sorry, my english is bad, hope you can understand what is my question

asked Jul 8, 2021 at 14:51
3
  • arr.tags is an array - if you want to access inside it, you can access it with arr.tags[n] Commented Jul 8, 2021 at 14:54
  • 3
    tags is an array such as array (not the best name...). To get every element of array you use .forEach(). Simply do the same with .tags Commented Jul 8, 2021 at 14:55
  • Do you want to iterate all tags, from name A and from name B, or just a specific one? Commented Jul 8, 2021 at 15:00

1 Answer 1

2

let array = [
 {name: "A", tags: ["a", "b"]}, 
 {name: "B", tags: ["a", "b", "c"]}
]
array.forEach(function(arr){
 var element = arr.name + " : ";
 arr.tags.forEach(function(tag){
 element = element + ('<span>'+tag+'</span>');
 });
 console.log(element);
});

answered Jul 8, 2021 at 15:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.