从文章:
发送一个JSON数组作为Dictionary<string,string>接收
我试图做同样的事情,因为那篇文章,唯一的问题是,我不知道什么键和值是前面。我需要动态添加键和值对,我不知道怎么做。
有人知道如何创建对象并动态添加键值对吗?
我试过了:
var vars = [{key:"key", value:"value"}];
vars[0].key = "newkey";
vars[0].value = "newvalue";
但这行不通。
从文章:
发送一个JSON数组作为Dictionary<string,string>接收
我试图做同样的事情,因为那篇文章,唯一的问题是,我不知道什么键和值是前面。我需要动态添加键和值对,我不知道怎么做。
有人知道如何创建对象并动态添加键值对吗?
我试过了:
var vars = [{key:"key", value:"value"}];
vars[0].key = "newkey";
vars[0].value = "newvalue";
但这行不通。
当前回答
JavaScript的Object本身就像一个字典。没有必要重新发明轮子。
var dict = {};
// Adding key-value -pairs
dict['key'] = 'value'; // Through indexer
dict.anotherKey = 'anotherValue'; // Through assignment
// Looping through
for (var item in dict) {
console.log('key:' + item + ' value:' + dict[item]);
// Output
// key:key value:value
// key:anotherKey value:anotherValue
}
// Non existent key
console.log(dict.notExist); // undefined
// Contains key?
if (dict.hasOwnProperty('key')) {
// Remove item
delete dict.key;
}
// Looping through
for (var item in dict) {
console.log('key:' + item + ' value:' + dict[item]);
// Output
// key:anotherKey value:anotherValue
}
小提琴
其他回答
JavaScript的Object本身就像一个字典。没有必要重新发明轮子。
var dict = {};
// Adding key-value -pairs
dict['key'] = 'value'; // Through indexer
dict.anotherKey = 'anotherValue'; // Through assignment
// Looping through
for (var item in dict) {
console.log('key:' + item + ' value:' + dict[item]);
// Output
// key:key value:value
// key:anotherKey value:anotherValue
}
// Non existent key
console.log(dict.notExist); // undefined
// Contains key?
if (dict.hasOwnProperty('key')) {
// Remove item
delete dict.key;
}
// Looping through
for (var item in dict) {
console.log('key:' + item + ' value:' + dict[item]);
// Output
// key:anotherKey value:anotherValue
}
小提琴
既然你已经声明,你想要一个字典对象(而不是一个数组,像我假设一些理解),我认为这是你想要的:
var input = [{key:"key1", value:"value1"},{key:"key2", value:"value2"}];
var result = {};
for(var i = 0; i < input.length; i++)
{
result[input[i].key] = input[i].value;
}
console.log(result); // Just for testing
对var dict ={}的改进是使用var dict = Object.create(null)。
这将创建一个没有object的空对象。原型就是原型。
var dict1 = {};
if (dict1["toString"]){
console.log("Hey, I didn't put that there!")
}
var dict2 = Object.create(null);
if (dict2["toString"]){
console.log("This line won't run :)")
}
你可以创建一个类Dictionary,这样你就可以轻松地与Dictionary列表交互:
类字典{ 构造函数(){ 这一点。Items = {}; } (关键){ 在this.items中返回键; } 集(关键字,值){ 这一点。项目[key] = value; } 删除(关键){ If (this.has(key)){ 删除this.items(例子) 返回true; } 返回错误; } } var d = new Dictionary(); d.set(“value1”) “value2 d.set (2) d.set(3,“value3”) console.log (d.has (2)); d.delete (2); console.log (d.has (2));
我遇到了这个问题。但是在for循环中。上面的解决方案不起作用(当使用变量(而不是字符串)作为push函数的参数时),其他解决方案没有考虑基于变量的键值。我很惊讶这种方法(这是常见的php)工作..
// example dict/json
var iterateDict = {'record_identifier': {'content':'Some content','title':'Title of my Record'},
'record_identifier_2': {'content':'Some different content','title':'Title of my another Record'} };
var array = [];
// key to reduce the 'record' to
var reduceKey = 'title';
for(key in iterateDict)
// ultra-safe variable checking...
if(iterateDict[key] !== undefined && iterateDict[key][reduceKey] !== undefined)
// build element to new array key
array[key]=iterateDict[key][reduceKey];