Skip to content

Instantly share code, notes, and snippets.

@jhyang12345
Created July 11, 2017 02:16
Show Gist options
  • Save jhyang12345/4b881f87aecd6c666d0c2b91c5f4c060 to your computer and use it in GitHub Desktop.
Save jhyang12345/4b881f87aecd6c666d0c2b91c5f4c060 to your computer and use it in GitHub Desktop.
const widget = {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"deeper": {"final": 400, "moredeeper": {"finally": 500}},
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
/*
function numberValueArray(widget) {
var arr = [];
Object.keys(widget).forEach((k1) => {
if ((typeof widget[k1] === 'object')) {
Object.keys(widget[k1]).forEach((k2) => {
if (typeof widget[k1][k2] === 'number') {
//console.log(k2);
arr.push(k2);
}
});
};
});
return arr;
}
*/
function numberValueArray(widget) {
var ret = [];
for(key in widget) {
if(typeof widget[key] === 'object') {
for(key2 in widget[key]) {
if(typeof widget[key][key2] === 'number') {
ret.push(key2);
}
}
}
}
return ret;
}
function recursiveSearch(widget) {
var ret = [];
var stack = [];
var index = 0;
//(key, value)
//if value is an object
//go deeper
for(key in widget) {
stack.push([key, widget[key]]);
}
while(index < stack.length) {
if (typeof stack[index][1] === 'number') {
ret.push(stack[index][0]);
} else if (typeof stack[index][1] === 'object') {
for (key in stack[index][1]) {
stack.push([key, stack[index][1][key]]);
}
} else {
}
index++;
}
return ret;
}
console.log(typeof widget);
console.log(numberValueArray(widget));
console.log(recursiveSearch(widget));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment