gpt4 book ai didi

javascript - 通过键获取对象,然后从同一对象的其他键获取值

转载 作者:行者123 更新时间:2023-12-03 05:05:45 24 4
gpt4 key购买 nike

我有一个对象数组

"fields": [
{
"label": "Country",
"name": "country",
"picklistValues": ["USA", "CAN"],
"type": "string"
},
{
"label": "Day",
"name": "day",
"picklistValues": ["Monday", "Tuesday", "Wednesday"],
"type": "string"
},
{...},
]

使用此代码

  renderTerritoryMeta() {
return this.props.territoryMeta.map((territoryField) => {
return (
<div className="slds-grid slds-box slds-box--x-small tile">
{territoryField.label}

</div>
);
});
}

enter image description here

它正在渲染数组中的所有对象。我想通过键 name 加上值来获取特定对象,然后获取同一对象的 picklistValues

// with this array
// get the object by key 'name' & value 'country'
// then with same object get values from key 'pickListValues'

最佳答案

您可以在 div 渲染之前使用条件我会尝试这样的事情:

renderTerritoryMeta() {
return this.props.territoryMeta.map((territoryField) => {
const shouldRender = territoryField.name === 'country'
let pickListValues = []
if (shouldRender) pickListValues = territoryField.pickListValues
if (!shouldRender) return null
return (
<div className="slds-grid slds-box slds-box--x-small tile">
{territoryField.label}

</div>
);
});
}

关于javascript - 通过键获取对象,然后从同一对象的其他键获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42011546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com