表单推送对象数组

时间:2016-06-10 14:14:30

标签: javascript jquery arrays object

我有这个数组/对象数组。我不知道它的正确名称..

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}
               ]
           }
        }
    }
}

} };

现在我有了这个表格

<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);

 });

我想表单推送到对象/数组中。例如fill在:

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

这是可能的吗?如果是,可以使用什么功能?

我想的是:

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;

(见here

但我似乎无法弄明白......

1 个答案:

答案 0 :(得分:0)

更改为:

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

         });
    });