我想根据我从服务器获取的数据生成这种结构。数组看起来像这样
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
}]
所以我想发送带有以这种格式选择的主题的请求。
{{ base_url }}/listings?num_items=1&start=3&topics[]=food&topics[]=home
这就是我要添加的&topics [] = food&topics [] = home,所以从this.topics中我需要选择:true并命名并添加该主题[]
答案 0 :(得分:2)
您可以先使用selected
基于Array#filter
键来过滤主题,然后在过滤后的数组上应用Array#reduce
来生成URL字符串:
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);
答案 1 :(得分:2)
您可以将主题映射到其名称,并在前面添加s="rest/dashboard/person/hari/category/savingaccount/type/withdraw"
print(s:match("person/([^/]+)/category/"))
print(s:match("category/([^/]+)/type/"))
,然后通过topics[]=
将其加入,然后将其添加到url:
&