Ubah suai sifat tunggal objek dalam Peta
P粉884667022
P粉884667022 2023-08-03 10:24:02
0
1
449
<p>Andaikan saya mempunyai jenis data yang dipetakan. </p> <pre class="brush:php;toolbar:false;">testMap: Peta<rentetan, sebarang></pre> <p>Nilai rentetan ialah kunci saya dan sebarang nilai ialah objek dalam peta itu. <br /><br />Objek mungkin kelihatan seperti ini:</p><p><br /></p> <pre class="brush:php;toolbar:false;">{ name: 'testName', umur: 20}</pre> <p>Andaikan pengguna memilih elemen dengan kunci melalui menu lungsur. <br /><br />Bagaimanakah saya sekarang boleh menukar nama objek kepada kekunci yang sepadan dengan kekunci ini? <br /><br />Saya telah mengulangi peta menggunakan gelung forEach dan cuba menukar sifat menggunakan Map.get() dan Map.set(). Malangnya, ini tidak berjaya. </p><p><br /></p>
P粉884667022
P粉884667022

membalas semua(1)
P粉301523298

Adakah begini

// Assuming you have the testMap already defined
// testMap: Map<string, any>

// Step 1: Get the selected key from the dropdown (replace 'selectedKey' with the actual selected key)
const selectedKey = 'someKey';

// Step 2: Retrieve the corresponding object from the map
const selectedObject = testMap.get(selectedKey);

// Step 3: Update the "name" property of the object
if (selectedObject) {
  selectedObject.name = selectedKey;
} else {
  // Handle the case when the selected key is not found in the map
  console.error('Selected key not found in the map!');
}

// Step 4: Set the updated object back into the map using the same key
testMap.set(selectedKey, selectedObject);

// Now, the "name" property of the selected object in the map should be updated to the selected key.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!