I am trying to create a simngle array of multiple values obtained from an input array of objects. Ideally I'd like to use "ES5" features to do this.
I need to transform an object such as this:
{
image: "bat",
count: 5
}
into this array [bat, bat, bat, bat, bat]
I think the code is the fastest way to explain:
//game object state
var gameObj = {
objects: [
{
image: "bat",
count: 5
},
{
image: "spider",
count: 4
},
{
image: "frankie",
count: 3
}
],
imagesArr: [],
}
function gameObjectArrayBuilder(obj) {
var resultArr = [];
resultArr = obj.map(buildArr);
function buildArr(prop) {
var image = prop.image;
var count = prop.count;
while (prop.count > 0) {
prop.count--
return prop.image
}
}
return resultArr;
}
gameObj.imagesArr = gameObjectArrayBuilder(gameObj.objects);
//View result
var html = document.querySelector("#content");
html.innerHTML = gameObj.imagesArr;
console.log(gameObj.imagesArr)
//Result should be
//[bat, bat, bat, bat, bat, spider, spider, spider, spider, frankie, frankie, frankie]
<div id="content">
</div>