0

I've got this array / object array. I don't know the proper name for it..

var enemy = {
'level1' : {
    creature :
    {
        creature_name : {
             'Goblin' : {
                info: {
                    'c_name' : 'Goblin',
                    'HP' : '30',
                    'damage' : '3',
                    'loot' : [
                        {name: 'a wooden sword'   , item: 'weapon'  , value: 2}, 
                        {name: 'a golden necklace', item: 'amulet' , value: 1},
                        {name: 'a pair of boots'  , item: 'boots'  , value: 1},
                        {name: 'some cloth legs'  , item: 'legs'  , value: 1},
                        {name: 'a cloth helmet'   , item: 'helm'  , value: 1}
                    ]
                }
             },
             'Cow' : {
                info: {
                    'c_name' : 'Cow',
                    'HP' : '10',
                    'damage' : '1',
                    'loot' : [
                        {name: 'bell'              , item: 'weapon'  , value: 0}, 
                        {name: 'cow hide cloak'    , item: 'cape'  , value: 1}, 
                        {name: 'a wooden sword'    , item: 'weapon'  , value: 2}, 
                        {name: 'a golden necklace' , item: 'amulet' , value: 1},
                    ]
                }
             },
             'Rat' : {
                info: {
                    'c_name' : 'Rat',
                    'HP' : '15',
                    'damage' : '2',
                    'loot' : [
                        {name: 'rait tail'            , item: 'cape'   , value: 1}, 
                        {name: 'an iron sword'        , item: 'weapon' , value: 3}, 
                        {name: 'a golden necklace'    , item: 'amulet' , value: 1},
                        {name: 'a pair of solid boots', item: 'boots'  , value: 2},
                    ]
                }
             },
        }
   
    },
},
'level2' : {
    creature :
    {
        creature_name : {
        'Dragon' : {
           info: {
               'c_name' : 'Dragon',
               'HP' : '100',
               'damage' : '5',
               'loot' : [
                   {name: 'an almighty dragon sword'   , item: 'weapon'  , value: 5}, 
                   {name: 'a dragon tooth', item: 'amulet' , value: 5},
                   {name: 'a pair of dragon boots'  , item: 'boots'  , value: 4},
                   {name: 'a dragon helmet'  , item: 'helm'  , value: 4}
               ]
           }
        }
    }
}

} };

Now I've got this form

<div id="insert_form">
<input type="text" placeholder="name" id="name" name="name">

<input type="text" placeholder="hp" id="hp" name="hp">

<input type="text" placeholder="damage" id="damage" name="damage">

<div id="insert">Add loot</div>

<input type="submit" value="Submit" id="submit">
lootname_array = [];
$('#insert').on('click', function(){

$("#insert_info_form").find('#insert').before('<input style="width:25%;" type="text" placeholder="★ lootname ★" class="lootname" name="loot"><input style="width:25%;margin-left:2%;" type="text" placeholder="★ lootitem ★" class="lootitem" name="lootitem"><input style="width:25%;margin-left:2%;" type="text" placeholder="★ lootvalue ★" class="lootvalue" name="lootvalue"><br />');

});

$('#submit').on('click', function(){
$(".lootname").each(function () {                  
    var lootname = $(this).val(); 
    lootname_array.push(lootname);

 });

And I would like to push the values of the form into the object / array For example fill in :

level = 1
name = test,
HP = 10,
damage = 50
loot = {name - testName} {item - testItem} {value=2}

Is this possible, and if so, with what function?

I thought something like:

var data = [];
// ...
data[0] = { "ID": "1", "Status": "Valid" };
data[1] = { "ID": "2", "Status": "Invalid" };
// ...
var tempData = [];
for ( var index=0; index<data.length; index++ ) {
    if ( data[index].Status == "Valid" ) {
        tempData.push( data );
    }
}
data = tempData;

(as seen here)

But I can't seem to figure it out..

Community
  • 1
  • 1
StabDev
  • 741
  • 3
  • 7
  • 17

1 Answers1

0

Change to:

 $('#submit').on('click', function(){
        $(".lootname").each(function () {                  
            lootnameObj = {};
            lootnameObj[$(this).attr('name')] = $(this).attr('name').val();
            lootname_array.push(lootnameObj);

         });
    });
Raghav Rangani
  • 843
  • 7
  • 23
  • I used an _example_ from [this](http://stackoverflow.com/questions/8925820/javascript-object-push-function) post im not using it _myself_ – StabDev Jun 10 '16 at 14:24