如何在声明的对象中添加具有相同键名的新属性? [英] How to add new property with same key name inside declared object?

查看:183
本文介绍了如何在声明的对象中添加具有相同键名的新属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I append/add new property with same key name inside declared object ?

这里代码:

// 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' }
];

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

What i've been try so far:

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

结果仍然无效。它只是取代现有的房产价值

The result still both doesn't work either. It just replace existing property value

我需要的是这样的:

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

请注意:我不想让它成为数组:爱好:['读','写']

Please note: I don't want to make it array : hobby: ['reading', 'writing']

更新:

我是为搜索功能实现的,上面的代码和详细信息是简化版的第一部分被卡住了。

I was implement this for search functionality, code and details above is simplified version of the part I was stuck.

这里是我试图完成的完整代码:

and here is the full code of what i've try to accomplish:

我的观点:

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

my viewmodels:

my 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会请求这样的东西:

So, ajax will request something like this:

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

有人,请帮助,谢谢:)

Someone, please help, Thanks :)

推荐答案

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

As the comments say, you can not have multiple property with the same name.

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

What you can do this to add the hobby value to the same hobby property and split it when you need all your hobbies:

var person = { hobby: "programmer" };

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

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

这篇关于如何在声明的对象中添加具有相同键名的新属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆