Ändern Sie eine einzelne Eigenschaft eines Objekts in einer Karte
P粉884667022
P粉884667022 2023-08-03 10:24:02
0
1
448
<p>Angenommen, ich habe einen zugeordneten Datentyp. </p> <pre class="brush:php;toolbar:false;">testMap: Map<string, any></pre> <p>Der Stringwert ist mein Schlüssel und jeder Wert ist ein Objekt in dieser Karte. <br /><br />Das Objekt könnte so aussehen:</p><p><br /></p> <pre class="brush:php;toolbar:false;">{ name: 'testName', age: 20}</pre> <p>Angenommen, der Benutzer wählt ein Element mit einer Taste über ein Dropdown-Menü aus. <br /><br />Wie kann ich nun mit diesem Schlüssel den Namen des Objekts in den entsprechenden Schlüssel ändern? <br /><br />Ich habe die Karte mithilfe einer forEach-Schleife durchlaufen und versucht, die Eigenschaften mithilfe von Map.get() und Map.set() zu ändern. Leider hat das nicht funktioniert. </p><p><br /></p>
P粉884667022
P粉884667022

Antworte allen(1)
P粉301523298

这样的吗

// 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.

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!