İç içe geçmiş bir nesneyi yok eden bir dizi oluşturma

0

Soru

Bir dizi oluşturmam gerekiyor id:'içinde s entity: içinde iç içe geçmiş değişken stakes nesnedeki değişken proposal. Nesneyi yok etmek için bu kodu kullandığımda:

const {
   stakes: {
      entity: { id: SupportersId },
    },
 } = proposal
  console.log(SupportersId)

Hatası alıyorum : TypeError: Cannot read property 'id' of undefined

Bu, kodu uyguladığım teklif nesnesi içinde iç içe geçmiş bir `stakes' değişken örneğidir, bu da çalışan çıktıdır:

const {stakes} = proposal
console.log(stakes)

çıktı:

0: 
 amount: BigNumber 
    c: [100000] 
    e: 19 
    s: 1 
    __proto__: Object 
 createdAt: 1616006864000 
 entity: {id: "0x60893...734",
        __typename: "Supporter"} 
 id: "appAddress:0x8blahblah...entity:0x608same" 
 proposal: null 
 type: "Add" 

Koşarken:

const {stakes: {entity}, }= proposal 
console.log(entity)

Alırım:

4 Undefined
10 Undefined
10 Undefined
...

Bu bir yinelemedir console.log(JSON.stringify(proposal))

{"id":"28","number":"28",
    "creator":"0x6089368594bf2d3734",
    "status":"Disputed","createdAt":1621957554000,
    "executedAt":0,"metadata":"test","link":"https://",
"stakes":[{"amount":"658768115942029002",
    "createdAt":1626278001000,
    "entity":{"id":"0x80db3372988d852a"},
    "id":"appAddress:0x0ccdedf21399-proposalId:0x1c- 
          entity:0x80db3aeb089372988d852a",
    "type":"Withdraw","proposal":null}],
"stakesHistory":[{"conviction":"0","createdAt":1622175449000,
    "entity":{"id":"0x80db3a571952988d852a"},
    "id":"appAddress:0x0cca32354b5da7dedf21399-proposalId:0x1c- 
          entity:0x80db3a571372988d852a-time:8662647",
"time":"8662647","tokensStaked":"86275362318840579720",
"totalTokensStaked":"86275362318840579720","type":"Add","proposal":null},
{"conviction":"2.156363897696420943854e+21",
"createdAt":1622175824000,"entity":{"id":"0x80db3a5719988d852a"},
"id":"appAddress:0x0ccda7dedf21399-proposalId:0x1c-entity:0x80db3a5b089372988d852a-time:8662672","time":"8662672","tokensStaked":"86202898550724637692",
"totalTokensStaked":"86202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"3.8533152129053758738146e+22",
"createdAt":1622182185000,
"entity":{"id":"0x80db3a57
2988d852a"},
"id":"appAddress:0x0cc6f0962b415
f21399-proposalId:0x1c-entity:0x80db3a5
72988d852a-time:8663096","time":"8663096","tokensStaked":"84202898550724637692",
"totalTokensStaked":"84202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.114296303522575335279954e+24",
"createdAt":1622406093000,"entity":{"id":"0x80db3a57195616eb3ccbb2b6eb089372988d852a"},"id":"appAddress:0x0cc6
1399-proposalId:0x1c-entity:0x80d
089372988d852a-time:8678021","time":"8678021","tokensStaked":"64202898550724637692",
"totalTokensStaked":"64202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.1372042
9e+24","createdAt":1622414360000,"entity":{"id":"0x80db3a
852a"},"id":"appAddress:0x0cc6
dedf21399-proposalId:0x1c-entity:0x80db3a
852a-time:8678572","time":"8678572","tokensStaked":"4202
37692","totalTokensStaked":"4202898550724637692","type":"Withdraw",
"proposal":null},{"conviction":"1.056
011e+24","createdAt":1622481968000,"entity":{"id":"0x8
2988d852a"},"id":"appAddress:0x0cc6
21399-proposalId:0x1c-entity:0x80
372988d852a-time:8683078","time":"8683078","tokensStaked":"2202898550724637692",
"totalTokensStaked":"2202898550724637692","type":"Withdraw","proposal":null},
{"conviction":"2.91692914179098548409009e+23","createdAt":1623713056000,"entity":{"id":"0x80db3a5
d852a"},"id":"appAddress:0x0cc
df21399-proposalId:0x1c-entity:0x80db3a57
8d852a-time:8765118","time":"8765118","tokensStaked":"2180833333333333347",
"totalTokensStaked":"2180833333333333347","type":"Withdraw","proposal":null},
{"conviction":"1.14417968316260986028367e+23","createdAt":1626276470000,"entity":{"id":"0x80d
8d852a"},"id":"appAddress:0x0cc6f0
f21399-proposalId:0x1c-entity:0x80db3
8d852a-time:8935883","time":"8935883","tokensStaked":"680833333333333347",
"totalTokensStaked":"680833333333333347","type":"Withdraw","proposal":null},
{"conviction":"1.14253001105365595015519e+23","createdAt":1626278001000,"entity":{"id":"0x80db3a57195616eb3ccbb2b6eb089372988d852a"},"id":"appAddress:0x0c
1399-proposalId:0x1c-entity:0x80d
d852a-time:8935985","time":"8935985","tokensStaked":"658768115942029002",
"totalTokensStaked":"658768115942029002","type":"Withdraw","proposal":null},
{"conviction":"3.2774663072910697629524e+22","createdAt":1636282624000,
"entity":{"id":"0x74b232255dc
5951d"},"id":"appAddress:0x0c
1399-proposalId:0x1c-entity:0x74b2
375951d-time:9600096","time":"9600096","tokensStaked":"1000000000000000000",
"totalTokensStaked":"1658768115942029002","type":"Add","proposal":null},
{"conviction":"3.3231555602660371350587e+22","createdAt":1636289513000,"entity":{"id":"0x74b232255
51d"},"id":"appAddress:0x
21399-proposalId:0x1c-entity:0x74b2
75951d-time:9600555","time":"9600555","tokensStaked":"0",
"totalTokensStaked":"658768115942029002","type":"Withdraw","proposal":null}],"beneficiary":"0x000000
000","requestedAmount":"0","totalTokensStaked":"658768115942029002",
"stable":false,"setting":null,"startDate":null,"totalPower":null,
"snapshotBlock":"0","yeas":null,"nays":null,"quietEndingExtensionDuration":null,
"quietEndingSnapshotSupport":null,"script":null,"isAccepted":null,
"casts":[],"actionId":"30","challengeId":"17","challenger":"0xc
020c","challengeEndDate":1622295426000,"disputeId":"10","settledAt":0,
"settlementOffer":"100000000000000000","disputedAt":1622036751000,
"pausedAt":1622036226000,"pauseDuration":0,"collateralRequirement":{"id":"appAddress:0x0cc6f
1399-proposalId:0x1c","proposalId":"appAddress:0x0cc6f
edf21399-proposalId:0x1c","tokenId":"0x3050
a4f9","tokenDecimals":18,"tokenSymbol":"HNYT","actionAmount":"100000000000000000",
"challengeAmount":"100000000000000000","challengeDuration":"259200"},
"submitterArbitratorFee":null,"challengerArbitratorFee":null,"name":"test",
"convictionTrend":"-2.248532625135e-8","currentConviction":"3.28405338806e+22",
"futureConviction":"3.2774+22","futureStakedConviction":"0.00010213206901472402",
"hasEnded":false,"maxConviction":"3.2090344832144279e+26",
"stakedConviction":"0.0001023377406715919","statusData":{"disputed":true},"userConviction":"0","userStakedConviction":"0","requestedAmountConverted":"0"}

Umarım biri bana bu konuda yardımcı olabilir. Değişkene, bir öğe oluşturmak için bir diziyi eşlerken yaptığınız gibi bir dizin numarası eklemem gerektiğini hissediyorum, ancak ne yazık ki bunun neye benzeyeceğinden de emin değilim. Nihayetinde, bu listeyi bir çocuk olarak, uygulamamdaki başka bir formda, görüntü ve profil verilerini kimlikten çekip "kişisel bir kart" oluşturmak için eşleyecek başka bir işleve aktaracağım.bu yardımcı olursa. Herhangi bir yardım veya tavsiye çok takdir edilecektir, teşekkürler!

1

En iyi cevabı

1

İşte doğru şekilde yok eden bir örnek. Bu örnekte, yalnızca ilk nesnenizi yok ediyorum. proposal.stakes dizi (örn.: proposal.stakes[0]).

const proposal = {
    stakes: [{
        amount: 10n,
        c: [100000],
        e: 19,
        s: 1,
        createdAt: 1616006864000,
        entity: {
            id: "0x60893...734",
            __typename: "Supporter"
        },
        id: "appAddress:0x8blahblah...entity:0x608same",
        proposal: null,
        type: "Add",
    }]
};

const {
    stakes: [{
        entity: { id: SupportersId },
    }],
} = proposal;
console.log(SupportersId)

const {stakes: [{entity}], }= proposal;
console.log(entity);

2021-11-24 05:17:45

Öneri nesnesi, arka uçtan aktarıldığım bir şeydir. Uygulamada daha sonra başka bir bileşenle eşlemek için SupportersId değişkenini oluşturmak istiyorum.
Striker

@Forvet, Ben aslında hizalamak için benim yazı güncellendi senin stakes özellik bir nesne dizisidir.
Wyck

Wyck, adam sensin! Neyi kaçırdığımı gösterdiğin için çok teşekkürler. Çabanı ve bilgini takdir ediyorum! Şerefe
Striker

@Striker, ben takdir geribildirim! Bu cevabı kabul etmek için onay işaretini tıklayabilirsiniz; bu, Stack Overflow'da "sen adamsın" dememizin tipik yoludur. ;)
Wyck

yeni bir kullanıcı için birçok sınırlı işlev @- kullanıcı ve o çeki tıkla. Ama dedi, Ben mi olmalıydı :)
Striker

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................