admin管理员组文章数量:1026380
I am kind of very new to javascript world. I have a doubt. How do i add/append new values to existing hash. example
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']);
Now how do i dynamically add more elements to set_1 key? like to extend the set_1 with coffee tea.
Sorry if this question has been repeated and is very trivial, i did my search. I found one article realted to it,how to assign variable value as variable name in a hash?
I am kind of very new to javascript world. I have a doubt. How do i add/append new values to existing hash. example
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']);
Now how do i dynamically add more elements to set_1 key? like to extend the set_1 with coffee tea.
Sorry if this question has been repeated and is very trivial, i did my search. I found one article realted to it,how to assign variable value as variable name in a hash?
Share Improve this question edited May 23, 2017 at 11:55 CommunityBot 11 silver badge asked Jan 9, 2012 at 18:43 SaiSai 1132 silver badges11 bronze badges3 Answers
Reset to default 3Here, elements['set_1']
is just a normal Javascript array. The fact that it is a property of the elements
object is pletely immaterial to how it behaves. You can add items to it in the normal Javascript way: with push
:
elements['set_1'].push('coffee');
elements['set_1'].push('tea');
If you don't know what the property's name will be when you write your code, you can do exactly the same with a variable:
elements[setname].push('coffee');
elements[setname].push('tea');
elements['set_1'].push('foo');
If the value of set_1 is an array
you can do this:
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
elements[setname].push('new item');
alert(elements['set_1']);
However, if it is not an array, you will need to take different action.
I am kind of very new to javascript world. I have a doubt. How do i add/append new values to existing hash. example
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']);
Now how do i dynamically add more elements to set_1 key? like to extend the set_1 with coffee tea.
Sorry if this question has been repeated and is very trivial, i did my search. I found one article realted to it,how to assign variable value as variable name in a hash?
I am kind of very new to javascript world. I have a doubt. How do i add/append new values to existing hash. example
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']);
Now how do i dynamically add more elements to set_1 key? like to extend the set_1 with coffee tea.
Sorry if this question has been repeated and is very trivial, i did my search. I found one article realted to it,how to assign variable value as variable name in a hash?
Share Improve this question edited May 23, 2017 at 11:55 CommunityBot 11 silver badge asked Jan 9, 2012 at 18:43 SaiSai 1132 silver badges11 bronze badges3 Answers
Reset to default 3Here, elements['set_1']
is just a normal Javascript array. The fact that it is a property of the elements
object is pletely immaterial to how it behaves. You can add items to it in the normal Javascript way: with push
:
elements['set_1'].push('coffee');
elements['set_1'].push('tea');
If you don't know what the property's name will be when you write your code, you can do exactly the same with a variable:
elements[setname].push('coffee');
elements[setname].push('tea');
elements['set_1'].push('foo');
If the value of set_1 is an array
you can do this:
var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
elements[setname].push('new item');
alert(elements['set_1']);
However, if it is not an array, you will need to take different action.
本文标签: javascriptHow to append new elements to the same key in a hashStack Overflow
版权声明:本文标题:javascript - How to append new elements to the same key in a hash - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745635570a2160426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论