2017-05-28 7 views
0

Ich möchte in der Lage sein, ein Objekt oder mehrere Objekte mit einer Funktion zu aktualisieren, die den Speicherort von dem angibt, was ich im Object aktualisieren möchte. Ich bin mir nicht sicher, was die richtige Syntax dafür ist. Mein Versuch unten erstellt tatsächlich einen neuen Parameter im ursprünglichen Objekt, da ich stattdessen das vorhandene Feld des Benutzerobjekts aktualisieren möchte.Wie spezifizierst du Objektstandorte in Funktionsargument Javascript

var User = { 
    local: { 
    location: { 
     city: "", 
     state: "" 
    } 
    } 
} 

var Profile = { 
    location: { 
    city: "", 
    state: "" 
    } 
} 

function update(model, parameter, updatedLocation) { 
     return model[updatedLocation] = { 
     city: updatedLocation.city, 
     state: updatedLocation.state, 
    }; 
} 

update(User, ["local"]["location"], { city: "ny", state: "ny"}); 
update(Profile, ["location"], { city: "ny", state: "ny"}); 

console.log(User); 
console.log(Profile); 

JS Fiddle

+0

Der Grund Ihrer aktuellen Code wird ein neues Feld hinzuzufügen, ist die Art und Weise Sie zum „Parameter“ vorbei. Sie möchten eine Zeichenfolge (kein Array), und Sie können zwei Indizes nicht so kombinieren, wie Sie es haben. Zum Beispiel 'update (User [" local "]," location ", ...' und 'update (Profile," location ", ...'. Auch sollte deine Funktion 'model [parameter]', nicht 'model [updatedLocation] '. –

Antwort

1

Sie könnten dies geben ein Versuch. Grundsätzlich ist es eine generische Art, auf eine beliebige Ebene in einem Objekt zu gehen und Eigenschaften zu aktualisieren. Details im Code kommentiert

var User = { 
 
    local: { 
 
    location: { 
 
     city: "", 
 
     state: "" 
 
    } 
 
    } 
 
} 
 

 
var Profile = { 
 
    location: { 
 
    city: "", 
 
    state: "" 
 
    } 
 
} 
 

 
function update(obj, path, updatedProps) { 
 
    var objToUpdate = obj; 
 
    // Iterate down the path to find the required object 
 
    for (var i = 0; i < path.length; i++) { 
 
    if (objToUpdate[path[i]]) { 
 
     objToUpdate = objToUpdate[path[i]] 
 
    } else { 
 
     // If any path is not found, then no point continuing 
 
     objToUpdate = null; 
 
     break; 
 
    } 
 
    } 
 
    if (objToUpdate) { 
 
    var props = Object.keys(updatedProps); 
 
    for (var j = 0; j < props.length; j++) { 
 
     var prop = props[j]; 
 
     objToUpdate[prop] = updatedProps[prop] || objToUpdate[prop]; 
 
    } 
 
    } 
 
    return obj; 
 
} 
 

 
update(User, ["local", "location"], { 
 
    city: "ny", 
 
    state: "ny" 
 
}); 
 
update(Profile, ["location"], { 
 
    city: "ny", 
 
    state: "ny" 
 
}); 
 

 
console.log("User:", User); 
 
console.log("Profile:", Profile);

+0

Danke das funktioniert! – Shivam

0

Ähnlich wie die anderen Antwort hier, aber es spielt keine explizite Codierung der location Struktur erfordern:

function update(model, parameter, newVal) { 
    model[parameter] = newVal; 
} 

update(User["local"], "location", { city: "ny", state: "ny"}); 
update(Profile, "location", { city: "ny", state: "ny"}); 

Oder wenn Sie nur Standort zu aktualisieren, können Sie Vereinfachen Sie die Funktion:

function updateLocation(model, newVal) { 
    model.location = newVal; 
} 
Verwandte Themen