2017-01-16 72 views
0

我有一个映射(字符串 - >字符串),我想将其转换为列表以便能够调用“map”将其转换为其他值的列表。背景是,我想用它在一个reactjs组件中,像这样:将Iterable转换为javascript/typescript中的列表

<input type="select"> 
{props.options.entries().toList().map((type,i) => { 
       return <option value={type[0]} key={i}>{type[1]}</option> 
}) 
} 
</input> 

现在,entries()给了我一个迭代表。但它没有“toList”。我怎样才能做这个转换?

+1

您是否尝试过只是做'项() .MAP(...)'?你也错过了一个结束大括号只是FYI – mhodges

+1

我不明白这段代码。不匹配的花括号和括号。 – Ultimater

+0

对不起,我修正了 – Nathan

回答

0

在反应的组分render功能,

render: function() { 
    return (
    <Root> 
    ... 
    {props.options.entries().toList().map((type,i) => { 
    return <option value={type[0]} key={i}>{type[1]}</option> 
    }) 
    ... 
    </Root>) 
} 

是从什么不同:

render: function() { 
    let optionList = props.options.entries().toList().map((type,i) => { 
    return <option value={type[0]} key={i}>{type[1]}</option> 
    } 

    return (
    <Root> 
    ... 
    {optionList} 
    ... 
    </Root>) 
} 

并没有什么不同:

render: function() { 
    let optionList = [] 
    // iterate through this iterable(pseudo, as the iterable interface is needed) 
    let entries = props.options.entries() 
    while (entries.hasNext()) { 
    optionList.push(entries.next()) 
    } 
    optionList.map((type,i) => { 
    return <option value={type[0]} key={i}>{type[1]}</option> 
    } 

    return (
    <Root> 
    ... 
    {optionList} 
    ... 
    </Root>) 
}