私はJSの既存のコードに迷惑をかけています。コードは機能しているので、私はそれを変更するために急いで急いではありませんが、以下の重複は私を悩ます。この状況を回避するための通常の/推奨/正式な方法は何ですか?キー/データの重複を避ける
実際のシステムは、大規模/複雑な金融システムであるので、私は、問題を示し、最も基本的な例にそれを単純化しています
var colours={
red:{id:"red", vals:[1,0,0]},
green:{id:"green", vals:[0,1,0]},
grey:{id:"grey", vals:[0.5,0.5,0.5]}
// ...etc
};
// id needs to be known internally within the object - thus it is defined as a property.
// e.g:
colour.prototype.identify(console.log(this.id));
// id also needs to be used externally to find an object quickly.
// e.g:
function getcolour(s){return colours[s];}
// Although this works. It does mean duplicating data, with the theoretical possibility of a mismatch:
var colours={//...
blue:{id:"green", // oh dear...
これはどのように、通常は専門家によって処理されるのでしょうか?
は[lodash](httpsに見てみましょう渡ししたいと思います://lodash.com/)library –