Javascript 如何根据JSON数组长度映射下拉列表
这个问题是关于react本地移动应用程序的。我想知道如何根据API JSON数组长度值生成下拉列表 在我的例子中,输出值如下Javascript 如何根据JSON数组长度映射下拉列表,javascript,react-native,drop-down-menu,react-native-android,Javascript,React Native,Drop Down Menu,React Native Android,这个问题是关于react本地移动应用程序的。我想知道如何根据API JSON数组长度值生成下拉列表 在我的例子中,输出值如下 eg:- Choice 1 (Label of the Drop Down) -Sub Choice 1 . (Value Data) -Sub Choice 2 (Value Data) -Sub Choice 3 (Value Data) Choice 2 (Label of the secon
eg:- Choice 1 (Label of the Drop Down)
-Sub Choice 1 . (Value Data)
-Sub Choice 2 (Value Data)
-Sub Choice 3 (Value Data)
Choice 2 (Label of the second Drop Down)
-Sub Choice 1 . (Value Data)
-Sub Choice 2 . (Value Data)
-Sub Choice 3 . (Value Data)
像这样,若API响应给出选项1,选项2->,那个么我需要相应地生成2个下拉列表
这是我的代码,根据我的代码,我只得到API响应数组的最后一个选项和选项数据
我正在使用react native material下拉库,我需要相应地将其映射到JSON数组长度
if(responseText.success == true)
{
var count = Object.keys(responseText.data).length; //Getting Array Data Length
let drop_down_data = []; // For Dropdown box
for(var i=0;i<count;i++){
// Next Loop for Fetching Choice Items
var t_count = Object.keys(responseText.data[i].choiceItems).length;
for(var j=0;j<t_count;j++){
var Add_On_Name = responseText.data[i].name
console.log(Add_On_Name)
this.setState({addOn_name: Add_On_Name});
this.setState({riceTypeData:responseText.data[i].choiceItems[j].name});
drop_down_data.push({ value: responseText.data[i].choiceItems[j].name});
}
}
this.setState({ drop_down_data , progressDialog:false}); // Set the new state
}
if(responseText.success==true)
{
var count=Object.keys(responseText.data).length;//获取数组数据长度
让下拉菜单_data=[];//用于下拉框
对于(var i=0;我希望这有帮助
//This is the initial state.
state = {
dropdownsdata: []
};
//This is going into the api successcallback
if (responseText.success) {
let all_dropdownData = [];
for (let i = 0; i < responseText.data.length; i++) {
let ind_dropdownData = [];
for (let j = 0; j < responseText.data[i].choiceItems.length; j++) {
let Add_On_Name = responseText.data[i].choiceItems[j].name;
let temp = {};
temp.value = Add_On_Name;
ind_dropdownData.push(temp);
}
let temp_obj = {};
temp_obj.label = responseText.data[i].name;
temp_obj.data = ind_dropdownData;
all_dropdownData.push(temp_obj);
}
this.setState({ dropdownsdata: all_dropdownData });
}
//Function for rendering n number of dropdowns.
renderDropdowns = () => {
return this.state.dropdownsdata.map(e => {
return <Dropdown label={e.label} data={e.data} />;
});
};
render(){
return <View>{this.renderDropdowns()}</View>;
};
//这是初始状态。
状态={
下拉数据:[]
};
//这将进入api successcallback
if(responseText.success){
让所有_dropdownData=[];
for(设i=0;i{
返回此.state.dropDownData.map(e=>{
返回;
});
};
render(){
返回{this.renderDropdowns()};
};
谢谢您的回答!!!!这是正确的,而且我想将我的数组中的标签添加到下拉标签中。我怎么做呢?我想添加'responseText.data[i].name'到我的下拉标签accordingly@AkilaDevinda希望上面的最新编辑对您有所帮助。您可以从下拉列表中提供的以下方法中获得它。| | selectedIndex()-->获取选定索引-->编号| | | | selectedItem()-->获取选定项。-->对象||