0

I have two arrays, one having user ids and another having tags. ''' ["ELG001", "ELG002"] ["TG01", "TG02"]

''' am supposed to submit to an endpoint that expects a json in this format ''' "id_tags": [{ "id": "ELG001", "tag": "TG01,TG02" }, { "id": "ELG002", "tag": "TG01,TG02" } ] '''

How can I achieve to send the data in the expected format using map function

1 Answer 1

1

I created a simple snippet for you below based on your question that you want the first array to be the id and just add the second array as tags for your json.

const idArray = ["ELG001", "ELG002"];
const tagArray = ["TG01", "TG02"];
let tagArr = [];
idArray.forEach((el) => {
  tagArr.push({
    id: el,
    tag: tagArray.join(",")
  })
})
const tag = {
  id_tags: tagArr
}
console.log(tag);

Sign up to request clarification or add additional context in comments.

2 Comments

what if it is required in this format "id_tags": [{ "id": "ELG001", "tag": "TG01" }, { "id": "ELG001", "tag": "TG02" } { "id": "ELG002", "tag": "TG01" } { "id": "ELG002", "tag": "TG02" } ]
const idArray = ["ELG001", "ELG002"] // is supposed to be reporter Ids const tagArray = ["TG01", "TG02"] // is supposed to hold selected tags const tagArr = []; idArray.forEach((el) => { tagArray.forEach((ll) =>{ tagArr.push({reporter: el, tag: ll }) }) }) const reporterTags = { reporter_tags: tagArr } console.log(reporterTags);

Your Answer

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.