mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-12 04:31:04 +00:00
Updated JScript Enumerator (markdown)
parent
e988781229
commit
0681bd14ac
|
@ -7,35 +7,41 @@ For example, when accessing the value of an array, it is expressed as `arr(i)` i
|
|||
### Convert MS/JScript Enumerator to JSObject
|
||||
|
||||
```js
|
||||
// MS JScript Enumerator to Array
|
||||
if (!Enumerator.prototype.toArray) {
|
||||
Enumerator.prototype.toArray = function() {
|
||||
var a = [];
|
||||
for (; !this.atEnd(); this.moveNext()) {
|
||||
var x = {};
|
||||
var b = new Enumerator(this.item().Properties_);
|
||||
for (; !b.atEnd(); b.moveNext()) {
|
||||
var c = b.item();
|
||||
if (typeof c.value !== "unknown") {
|
||||
x[c.name] = c.value;
|
||||
} else {
|
||||
var i = 0, d = [];
|
||||
while (true) {
|
||||
try {
|
||||
d.push(c.value(i));
|
||||
i++;
|
||||
} catch (e) {
|
||||
break;
|
||||
}
|
||||
// The provided code snippet has been corrected by ChatGPT.
|
||||
// https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa
|
||||
// Related issues: welsonjs#75 welsonjs#42 welsonjs#30
|
||||
Enumerator.prototype.toArray = function() {
|
||||
var result = [];
|
||||
while (!this.atEnd()) {
|
||||
var currentItem = this.item();
|
||||
var currentItemProperties = currentItem.Properties_;
|
||||
var itemObject = {};
|
||||
|
||||
var propertiesEnumerator = new Enumerator(currentItemProperties);
|
||||
while (!propertiesEnumerator.atEnd()) {
|
||||
var property = propertiesEnumerator.item();
|
||||
if (typeof property.value !== "unknown") { // The type "Unknown" is Array
|
||||
itemObject[property.name] = property.value;
|
||||
} else {
|
||||
var arrayValues = [];
|
||||
var index = 0;
|
||||
while (true) {
|
||||
try {
|
||||
arrayValues.push(property.value(index));
|
||||
index++;
|
||||
} catch (e) {
|
||||
break;
|
||||
}
|
||||
x[c.name] = d;
|
||||
}
|
||||
itemObject[property.name] = arrayValues;
|
||||
}
|
||||
a.push(x);
|
||||
propertiesEnumerator.moveNext();
|
||||
}
|
||||
return a;
|
||||
};
|
||||
}
|
||||
result.push(itemObject);
|
||||
this.moveNext();
|
||||
}
|
||||
return result;
|
||||
};
|
||||
```
|
||||
|
||||
### References
|
||||
|
@ -43,4 +49,5 @@ if (!Enumerator.prototype.toArray) {
|
|||
* https://social.technet.microsoft.com/Forums/systemcenter/en-US/2a0078db-2053-4e21-9262-62ffbc156862/enumerating-fields-returned-with-a-wmi-query?forum=configmgrgeneral
|
||||
* http://www.java2s.com/Tutorial/JavaScript/0600__MS-JScript/Enumeratoritem.htm
|
||||
* https://stackoverflow.com/questions/973016/jscript-enumerator-and-list-of-properties
|
||||
* https://stackoverflow.com/questions/6346766/javascript-enumerator/6346909
|
||||
* https://stackoverflow.com/questions/6346766/javascript-enumerator/6346909
|
||||
* https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa
|
Loading…
Reference in New Issue
Block a user