Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 515 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to modify JavaScript object name?

#1
In JavaScript I have following code:

var rights = {
'One': [ 1, 1, 0],
'Two': [ 1, 0, 0],
'Three': [ 0, 1, 1 ]
};

And printing:

for (item in rights) Response.Write(item + ' = ' + rights[item] + '<br />');

Also, I can access any element in object by this:

rights['One'] or rights[0]

But how can I iterate through this object makeing indexes lowercased so that it becomes as follows:

var rights = {
'one': [ 1, 1, 0],
'two': [ 1, 0, 0],
'three': [ 0, 1, 1 ]
};
Reply

#2
var fieldNames = [];
for (item in rights) { fieldNames.push(item); }
for(i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var value = rights[fieldName];
delete rights[fieldName];

rights[fieldName.toLowerCase()] = value;
}
Reply

#3
for (var key in rights) {

key = key.toLowerCase();

}

here is fiddle:

[To see links please register here]


***
ok this doesn't actually update the key. just gives you the key in lowercase.
Reply

#4
You can't directly change a key. You can get the key and its value, set a new key with lowercase key value and then remove the original key.

var rights = {
'One': [ 1, 1, 0],
'Two': [ 1, 0, 0],
'Three': [ 0, 1, 1 ]
};

for (var key in rights) {
var keyLower = key.toLowerCase();
// if key is not already lower case
if (keyLower !== key) {
var temp = rights[key];
delete rights[key];
rights[keyLower] = temp;
}
}

// all keys in the rights object will now be lowercase

Working demo:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through