我使用 Fluent UI React 在我的 SPFx Extension 構建中有以下ContextualMenu結構:
const menuProps: IContextualMenuProps = {
items: [
{
key: 'Access',
itemType: ContextualMenuItemType.Section,
sectionProps: {
topDivider: true,
bottomDivider: true,
title: 'Sites you have access to',
items: [
{ key: 'DynamicValue1.1', text: 'DynamicValue1.2' },
{ key: 'DynamicValue2.1', text: 'DynamicValue2.2' },
],
},
},
],
};
我還運行了一個 MS Graph 呼叫,讓我獲得了一些 SharePoint 站點和團隊。
我現在想將這些動態回應推送到menuProps正確的位置。
所以基本上將動態陣列添加到嵌套items物件中。
items: [
{ key: 'DynamicValue1.1', text: 'DynamicValue1.2' },
{ key: 'DynamicValue2.1', text: 'DynamicValue2.2' },
],
我怎樣才能定位那個“物件”?(希望我理解正確,items是一個物件......)
有沒有辦法做到這一點array.push()?
uj5u.com熱心網友回復:
為了使這個庫不可知,它看起來像這樣:
const obj = {
items: [
{
key: 'Access',
itemType: '',
sectionProps: {
topDivider: true,
bottomDivider: true,
title: 'Sites you have access to',
items: [
{ key: 'DynamicValue1.1', text: 'DynamicValue1.2' },
{ key: 'DynamicValue2.1', text: 'DynamicValue2.2' },
],
},
},
],
};
obj.items[0].sectionProps.items.push({ key: 'DynamicValue3.1', text: 'DynamicValue3.2' })
console.log(obj.items[0].sectionProps.items)
你console.log會回傳這個:
[
{ key: 'DynamicValue1.1', text: 'DynamicValue1.2' },
{ key: 'DynamicValue2.1', text: 'DynamicValue2.2' },
{ key: 'DynamicValue3.1', text: 'DynamicValue3.2' }
]
如果您可以訪問menuProps: IContextualMenuProps,則只需替換obj為必要的變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/504546.html
標籤:javascript 反应 数组 目的 spfx
