打印键和值javascript对象

时间:2016-11-24 00:57:14

标签: javascript

我有两个数组,一个带有键,另一个带有值。我把它们组合成一个物体。我想从对象中选择几个(不是全部)键及其值并打印它们 - 键及其值。请不要使用jQuery或任何其他框架。这是我的例子:



keys = ["AB", "CD", "EF", "ABC", "ZZZ"];
values = ["1", "2", "9", "DB", "xxx"];

var dict = {};
for (var x=0; x<keys.length; x++){
	dict[keys[x]] = values[x];
}

console.log(dict['EF']);	//9
&#13;
&#13;
&#13;

如何将输出作为EF:9

2 个答案:

答案 0 :(得分:0)

尝试return Json(data, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings); 循环

&#13;
&#13;
for
&#13;
&#13;
&#13;

答案 1 :(得分:0)

循环键名并打印它们和dict值。

&#13;
&#13;
keys = ["AB", "CD", "EF", "ABC", "ZZZ"];
values = ["1", "2", "9", "DB", "xxx"];

var dict = {};
for (var x=0; x<keys.length; x++){
	dict[keys[x]] = values[x];
}

var keys_to_find = ['EF', 'ZZZ'];
keys_to_find.forEach(function(key) {
    console.log(key + ":" + dict[key]);
});
&#13;
&#13;
&#13;