Sto verificando gli attributi in un oggetto JavaScript, sostituendo alcune chiavi cancellando il prefisso "elemento" e salvando i nuovi valori in un altro oggetto.
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
key = keys[j].replace("element_", "");
switch(key) {
default :
tmp[key] = json[key];
break;
}
}
La questione è che quando lo faccio, posso registrare tutti i tasti, hanno i nomi corretti, ma quando provo a impostare i valori associati a questi tasti, non sono definiti (json [chiave]).
È dovuto al fatto che ho convertito le chiavi (Objects
) a Strings
(con il replace
metodo)?
risposte:
4 per risposta № 1Il problema è che stai cercando la proprietà nell'oggetto originale usando la nuova chiave. Uso keys[j]
invece di key
:
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
var key = keys[j].replace(/^element_/, "");
tmp[key] = json[keys[j]];
}
Io uso un'espressione regolare nella sostituzione in modo che ^
può abbinare l'inizio della stringa. In questo modo sostituisce solo la stringa quando è un prefisso e non gira ad esempio noexample_data
in no_data
.
Nota: quello che hai non è "un json", è un oggetto JavaScript JSON è un formato di testo per rappresentare i dati.
È dovuto al fatto che ho convertito le chiavi (oggetti) in stringhe (con il metodo di sostituzione)?
No. Le chiavi sono stringhe, non oggetti.
Puoi anche modificare le proprietà nell'oggetto originale eliminando quelle vecchie e aggiungendo nuove:
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
if (keys[j].indexOf("element_") == 0) {
json[keys[j].substr(8)] = json[keys[j]];
delete json[keys[j]];
}
}
22 per risposta № 2
Scriveremo una piccola funzione per fissare la chiave nel modo desiderato.
function fix_key(key) { return key.replace(/^element_/, ""); }
Sottolineare
_.object(
_.map(_.keys(json), fix_key),
_.values(json)
)
ES5 / loop
var keys = Object.keys(json);
var result = {};
for (i = 0; i < keys.length; i++) {
var key = keys[i];
result[fix_key(key)] = json[key];
}
return result;
ES5 / ridurre
Object.keys(json) . reduce(function(result, key) {
result[fix_key(key)] = json[key];
return result;
}, {});
ES6
Object.assign(
{},
...Object.keys(json) .
map(key => ({[fix_key(key)]: json[key]}))
)
Questo rende una serie di piccoli oggetti ciascuno con una coppia chiave-valore, usando la funzione "nome proprietà calcolato" ES6, quindi li passa a Object.assign
utilizzando l'operatore "spread" ES6, che li unisce insieme.
2 per risposta № 3
Prova questo:
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
var key = keys[j]; // key
var value = json[key]; // data
delete json[key]; // deleting data with old key
key = key.replace("element_", ""); // renaming key
json[key] = value; // setting data with new key
}
1 per risposta № 4
key
non è originale json
perché tu hai rimosso element_
prefisso
var keys = Object.keys(json);
for (var j=0; j < keys.length; j++) {
key = keys[j].replace("element_", "");
var _key = keys[j];
switch(key) {
default :
tmp[key] = json[_key];
break;
}
}