SORU
9 Aralık 2008, Salı


Dinamik olarak JavaScript ilişkisel dizi anahtarları oluşturma

Nasıl ben dinamik olarak javascript ilişkilendirilebilir diziler anahtarları oluşturabilir mi?

Şimdiye kadar bulduğum tüm belgeleri önceden oluşturulmuş anahtarları güncellemek için:

 arr['key'] = val;

" name = oscar " böyle bir dize var

Ve böyle bir şey ile bitirmek istiyorum:

{ name: 'whatever' }

Bu dize ayrıldım ve ilk eleman olsun ve bir sözlük koymak istiyorum.

Kod

var text = ' name = oscar '
var dict = new Array();
var keyValuePair = text.split(' = ');
dict[ keyValuePair[0] ] = 'whatever';
alert( dict ); // prints nothing.

CEVAP
9 Aralık 2008, Salı


Peki çalışırken nasıl olduysa tüm örnekler, overcomplicated

  • Bir overkill (bir yük) basit bir ilişkisel dizi (NAM-ı diğer sözlük) için new Array(), kullanıyorlar.
  • Daha iyilerini new Object() kullanın. Gayet iyi çalışıyor, ama neden tüm bu ekstra yazarak?

Bu soruyu tagged "acemi", bu yüzden, bu kadar basit.

JavaScript bir sözlük kullanmak için süper basit bir yolla ya da "Neden JavaScript özel sözlük bir nesne yok mu?":

// create an empty associative array (in JavaScript it is called ... Object)
var dict = {};   // huh? {} is a shortcut for "new Object()"

// add a key named fred with value 42
dict.fred = 42;  // we can do that because "fred" is a constant
                 // and conforms to id rules

// add a key named 2bob2 with value "twins!"
dict["2bob2"] = "twins!";  // we use the subscript notation because
                           // the key is arbitrary (not id)

// add an arbitrary dynamic key with a dynamic value
var key = ..., // insanely complex calculations for the key
    val = ...; // insanely complex calculations for the value
dict[key] = val;

// read value of "fred"
val = dict.fred;

// read value of 2bob2
val = dict["2bob2"];

// read value of our cool secret key
val = dict[key];

Şimdi değişim değerleri

// change the value of fred
dict.fred = "astra";
// the assignment creates and/or replaces key-value pairs

// change value of 2bob2
dict["2bob2"] = [1, 2, 3];  // any legal value can be used

// change value of our secret key
dict[key] = undefined;
// contrary to popular beliefs assigning "undefined" does not remove the key

// go over all keys and values in our dictionary
for (key in dict) {
  // for-in loop goes over all properties including inherited properties
  // let's use only our own properties
  if (dict.hasOwnProperty(key)) {
    console.log("key = "   key   ", value = "   dict[key]);
  }
}

Silme değerleri çok kolay

// let's delete fred
delete dict.fred;
// fred is removed, the rest is still intact

// let's delete 2bob2
delete dict["2bob2"];

// let's delete our secret key
delete dict[key];

// now dict is empty

// let's replace it, recreating all original data
dict = {
  fred:    42,
  "2bob2": "twins!"
  // we can't add the original secret key because it was dynamic,
  // we can only add static keys
  // ...
  // oh well
  temp1:   val
};
// let's rename temp1 into our secret key:
if (key != "temp1") {
  dict[key] = dict.temp1; // copy the value
  delete dict.temp1;      // kill the old key
} else {
  // do nothing, we are good ;-)
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 365multimedia

    365multimedi

    26 ŞUBAT 2009
  • Carlos Delgado

    Carlos Delga

    21 HAZİRAN 2011
  • Edgar flores

    Edgar flores

    7 HAZİRAN 2006