JSFiddle - React, Tailwind, and code Playground
HTML
<strong>Index:<strong>
<input type="text" id="input" size="3"/>
<input type="button" id="moveLeft" value="Move Left" />
<input type="text" id="output" />
JavaScript
Object.prototype.getByIndex = function (property, index) {
var n = 0;
if (this.hasOwnProperty(property)) {
for (var prop in this[property]) {
if (n == index) {
console.log('Found Match: ' + prop + ':' + this[property][prop]);
return this[property][prop];
} else {
n++;
}
}
}
return undefined;
}
$('#moveLeft').on('click', function (nr) {
var theBody = {
bodies: {
a: 'foo1',
b: 'foo2',
c: 'foo3'
}
};
var index = parseInt($('#input').val());
var currIndex = !isNaN(index) ? index : 0;
var newBody = theBody.getByIndex('bodies', currIndex);
$('#output').val(newBody);
});