admin管理员组文章数量:1405586
I have two HTML text input elements and one paragraph element. I have an empty javascript array and a function to push those two text input values as key:value into the array. I have a button to activate the function and print the array in the paragraph.
My javascript function is running but, the output is never producing [key:value],[key:value] but producing [object Object],[object Object]
HTML :
<input type="text" id="t1" />
<input type="text" id="t2" />
<button id="b1" onclick="add()">Push</button>
<p id="p1"></p>
JavaScript :
var t = [];
function add() {
var t1 = document.getElementById('t1').value;
var t2 = document.getElementById('t2').value;
t.push({key:t1, value:t2});
document.getElementById('p1').innerHTML=t;
}
How can I properly pushing key:value to this array and print it?
Update I ended up going with an Object
syntax that looks like this:
const m = {}
m[1] = 2
console.log(m, m[1])
I have two HTML text input elements and one paragraph element. I have an empty javascript array and a function to push those two text input values as key:value into the array. I have a button to activate the function and print the array in the paragraph.
My javascript function is running but, the output is never producing [key:value],[key:value] but producing [object Object],[object Object]
HTML :
<input type="text" id="t1" />
<input type="text" id="t2" />
<button id="b1" onclick="add()">Push</button>
<p id="p1"></p>
JavaScript :
var t = [];
function add() {
var t1 = document.getElementById('t1').value;
var t2 = document.getElementById('t2').value;
t.push({key:t1, value:t2});
document.getElementById('p1').innerHTML=t;
}
How can I properly pushing key:value to this array and print it?
Update I ended up going with an Object
syntax that looks like this:
const m = {}
m[1] = 2
console.log(m, m[1])
Share
Improve this question
edited Aug 11, 2020 at 18:07
WestCoastProjects
63.4k106 gold badges365 silver badges627 bronze badges
asked May 10, 2017 at 19:57
user7993717user7993717
2
-
arrays don't have key/value pairs, but objects sort of do (property names and values). so, t will need to be initialized as an object, ie
var t = {};
– James Commented May 10, 2017 at 20:00 - But when declare t={ } , it produce the output as [object Object]. Not [ t1 t2 ] – user7993717 Commented May 10, 2017 at 20:07
2 Answers
Reset to default 2You push elements in array correctly, the problem is conversion to string. You can see array in console.
function add() {
var t1 = document.getElementById('t1').value;
var t2 = document.getElementById('t2').value;
t.push({key:t1, value:t2});
console.log(t);
}
var t = [];
function add() {
var t1 = document.getElementById('t1').value;
var t2 = document.getElementById('t2').value;
t.push({key:t1, value:t2});
render()
}
function render() {
var html = '';
t.forEach(function(element) {
html += element['key'] +' = ' + element['value'] + '<br/>';
})
document.getElementById('p1').innerHTML = html;
}
<input type="text" id="t1" /> <input type="text" id="t2" /> <button id="b1" onclick="add()">Push</button> <p id="p1"></p>
本文标签: htmlHow to push keyvalue to dictionary in JavaScriptStack Overflow
版权声明:本文标题:html - How to push key:value to dictionary in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744907185a2631693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论