此API要求我发送一些逗号分隔的数据,在处理用户输入时,我使用如下复选框

   <SelectMultiple
      items={dairy}
      selectedItems={this.state.selectedIngredients}
      onSelectionsChange={this.onSelectionsChange} />


如果我在FlatList上渲染selectedIngredients(使用item.value),我绝对可以看到输入,但是当我尝试打印实际的url时,我得到了这个[object,Object]

我在视图中使用了this.state.selectedIngredients,结果如下:


  url.com/api /?= [对象对象],[对象对象],[对象对象]


在我的search函数中,我使用该代码来处理用户输入:

  const { selectedIngredients } = this.state;
  let url = "url.com/api/?i=" + selectedIngredients


在库的文档中,他们说所选项目是字符串或{标签,值}的类型数组,我使用那里提供的方法来追加所选项目:

  onSelectionsChange = (selectedIngredients) => {
    // selectedFruits is array of { label, value }
    this.setState({ selectedIngredients})
  }


我尝试在它们两个上都添加.value并不能解决我的问题。有人可以帮忙吗?

控制台日志this.state.selectedIngredients:

Array [
  Object {
    "label": "Goat cheese",
    "value": "Goat cheese",
  },
  Object {
    "label": "Cream cheese",
    "value": "Cream cheese",
  },
  Object {
    "label": "Butter",
    "value": "Butter",
  },
]

最佳答案

selectedIngredients = [
 {
    "label": "Goat cheese",
    "value": "Goat cheese",
  },
  {
    "label": "Cream cheese",
    "value": "Cream cheese",
  },
  {
   "label": "Butter",
    "value": "Butter",
  },
]

let selectedValues = selectedIngredients.map((ingredient)=> ingredient.value)
let selectedValuesInString = selectedValues.join(',');
console.log(selectedValuesInString)

10-08 06:21