如何在声明的对象中添加具有相同键名的新属性?

时间:2014-08-26 13:36:51

标签: javascript jquery knockout.js javascript-objects

如何在声明的对象中追加/添加具有相同键名的新属性?

这里是代码:

// Declared object
var myObject = {
    name : 'John',
    age  : 15,
};

// Array list of new property need to be add to myObject
var others = [
  { key : 'hobby', value: 'reading' },
  { key : 'hobby', value: 'writing' }
];

到目前为止我一直在尝试:

ko.utils.arrayForEach(others, function(other) {
    myObject[other.key] = other.value;
});

ko.utils.arrayForEach(others, function(other) {
    var extendObject = new Object();
    extendObject[other.key] = other.value;

    $.extend(myObject, extendObject);
});

结果仍然无法发挥作用。它只是取代现有的属性值

我需要的是这样的事情:

myObject: { name: 'John', age: 15, hobby: 'reading', hobby: 'writing' }

请注意:我不想制作数组:hobby: ['reading', 'writing']

更新

我是为搜索功能实现的,上面的代码和细节是我被卡住的部分的简化版本。

以下是我试图完成的完整代码:

我的观点:

<form data-bind="submit: $root.getSearchData">
  <input name="date_start" class="datepicker">

  <div class="checkbox">
    <label>
       <input type="checkbox" name="user_id[]" value="1"> John
    </label>
    <label>
       <input type="checkbox" name="user_id[]" value="2"> Michael
    </label>
  </div>
</form>

我的viewmodels:

// Will contains new search property need to append to queryData
self.searchPropertyList = ko.observableArray();

// This function will fetch tasks from server
self.getTasksFromServer = function()
{
   // Query data
   var queryData = {
       sort : 'start',
       order: 'asc'
   };

   /**
    * When getSearchData function fill formData into searchPropertyList observable,
    * this function will get a notify and it will automatically loop through it 
    * and append new property to queryData.
    */
   ko.utils.arrayForEach(self.searchPropertyList(), function(opt)
   {
       queryData[opt.name] = opt.value;
   }

   $.ajax({
       url: 'api/tasks',
       type: 'json',
       data: queryData,
       success: function(data)
       {
       }
   });
}

// Returned form property as array into searchPropertyList observableArray 
// and notify getTaskFromServer function
// This is how formData may look like
// formData = [{ name: 'date_start', value: '2014-08-26' }, { name: 'user_id[]', value: 1 }, { name: 'user_id[]', value: 2 }]; 
self.getSearchData = function(form)
{
     var formData = $(form).serializeArray();

     self.searchPropertyList(formData);
}

所以,ajax会请求这样的东西:

api/tasks?sort=start&order=asc&date_start=2014-08-26&user_id[]=1&user_id[]=2

有人,请帮助,谢谢:)

3 个答案:

答案 0 :(得分:2)

正如评论所说,你不能拥有同名的多个财产。

你可以做些什么来将爱好值添加到同一个爱好属性中,并在你需要所有爱好时拆分它:

var person = { hobby: "programmer" };

// adding another hobby
person.hobby =+ "-secretary";

var hobbies = person.hobby.split("-"); // [programmer,secretary]

答案 1 :(得分:0)

重复

How can I store multiple values per key in a hash table using Node.JS?

你必须使用{key,[array]}作为值的hashmap,并使用自定义方法来推送和弹出。

答案 2 :(得分:0)

无法在对象内添加/附加相同的键和差值,但您可以添加和额外的键,例如下面的代码。通过这样做,您可以区分相同的密钥

// Declared object
var myObject = {
    name : 'John',
    age  : 15,
};

// Array list of new property need to be add to myObject
var others = [
  {0 : { key : 'hobby', value: 'reading' }},
  {1 :{ key : 'hobby', value: 'writing' }}
];