javascript 如何在声明的对象中添加具有相同键名的新属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25507234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add new property with same key name inside declared object?
提问by Fariz Azmi
How can I append/add new property with same key name inside declared object ?
如何在声明的对象中附加/添加具有相同键名的新属性?
Here the code:
这里的代码:
// 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;
});
and
和
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
结果仍然两者都不起作用。它只是替换现有的属性值
What I need is something like this:
我需要的是这样的:
myObject: { name: 'John', age: 15, hobby: 'reading', hobby: 'writing' }
Please note: I don't want to make it array : hobby: ['reading', 'writing']
请注意:我不想让它成为数组: hobby: ['reading', 'writing']
UPDATE:
更新:
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:
这是我尝试完成的完整代码:
my views:
我的观点:
<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:
我的视图模型:
// 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);
}
So, ajax will request something like this:
所以,ajax 会请求这样的东西:
api/tasks?sort=start&order=asc&date_start=2014-08-26&user_id[]=1&user_id[]=2
Someone, please help, Thanks :)
有人请帮忙,谢谢:)
采纳答案by Misters
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]
回答by sbjumani
Duplicate of
重复的
How can I store multiple values per key in a hash table using Node.JS?
You have to use {key, [array]} which is hashmap with array as the value and write custom method to push and pop.
您必须使用 {key, [array]} ,它是以数组为值的哈希图,并编写自定义方法来推送和弹出。
回答by iNikkz
You cannot add/append same keyswith difference values inside an object but you can add and extra keysuch as below code. By doing this, you can differentiate the same keys:
您不能在对象内添加/附加具有不同值的相同键,但您可以添加额外的键,例如下面的代码。通过这样做,您可以区分相同的键:
// 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' }}
];