0

I want to generate this kind of structure based on the data which I am getting from server. The array looks like this

this.topics = [{
  "name": "beauty",
  "selected": false
}, {
  "name": "career",
  "selected": false
}, {
  "name": "childcare",
  "selected": false
}, {
  "name": "crafts",
  "selected": false
}, {
  "name": "culture",
  "selected": false
}, {
  "name": "fashion",
  "selected": true
}, {
  "name": "finances",
  "selected": true
}, {
  "name": "fitness",
  "selected": false
}, {
  "name": "food",
  "selected": false
}, {
  "name": "health",
  "selected": false
}, {
  "name": "home",
  "selected": true
}, {
  "name": "personal",
  "selected": false
}, {
  "name": "relationships",
  "selected": false
}, {
  "name": "religion",
  "selected": false
}]

So I want to send request with those topics which are selected in this format.

{{ base_url  }}/listings?num_items=1&start=3&topics[]=food&topics[]=home

This is what i want to add &topics[]=food&topics[]=home, so from this.topics I need to take selected: true and take name and add in that topics[]

4
  • 3
    Alright. What've you tried? Commented Aug 13, 2018 at 16:00
  • Can you please add the data with the question? Do you want to parse the backend data and get this array of objects? Please elaborate! Commented Aug 13, 2018 at 16:02
  • I didn't try yet anything, honestly I don't know what to do and I need help. You have data here which is this.topics, I just want to get from array objects which have selected: true and make a string for request Commented Aug 13, 2018 at 16:04
  • 1
    But food isnt selected? Commented Aug 13, 2018 at 16:10

2 Answers 2

2

You could map the topics to their name and add the topics[]= in front and join them by & and add that to the url:

   const topics = this.topics
      .filter(topic => topic.selected)
      .map(topic => "topics[]=" + topic.name)
      .join("&");
   const url = "/listings?num_items=1&start=3&" + topics;
Sign up to request clarification or add additional context in comments.

2 Comments

Nice readable solution. But it needs a filter on selected before mapping.
@kim yeah then just .filter() before...
2

You may filter the topics based on the selected key first using Array#filter and then apply Array#reduce on the filtered array to generate the URL string:

let topics = [{"name":"beauty","selected":false},{"name":"career","selected":false},{"name":"childcare","selected":false},{"name":"crafts","selected":false},{"name":"culture","selected":false},{"name":"fashion","selected":true},{"name":"finances","selected":true},{"name":"fitness","selected":false},{"name":"food","selected":false},{"name":"health","selected":false},{"name":"home","selected":true},{"name":"personal","selected":false},{"name":"relationships","selected":false},{"name":"religion","selected":false}];

let result = '{{ base_url  }}/listings?num_items=1&start=3';
result = topics.filter(item => item.selected)
  .reduce((string, { name }) => `${string}&topics[]=${name}`, result);

console.log(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.