我试图通过给出KEY来从KEY,VALUE对象中找到值。 关键是数组。
var arr = ["1398","1392","1390"];
var objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
我需要从键在数组中的对象中找到所有值。我怎样才能做到这一点 ? 到目前为止,我做到了这一点:
for (var a=0; a<arr.length;a++) {
console.log(arr[a]);
}
for (var k in objects){
if (typeof objects[k] !== 'function') {
console.log(k + ", " + objects[k]);
}
}
答案 0 :(得分:3)
您可以按objects[ arr[ 0 ] ]
查找返回systemConfiguration
0
,1
和2
是arr
的关键
你可以循环找到所有。喜欢:
var arr = ["1398", "1392", "1390"];
var objects = {
1384: "registerItem",
1386: "createGroups",
1388: "vipNumbers",
1390: "targetNumbers",
1392: "ignoredNumbers",
1394: "globalConstants",
1396: "Users",
1398: "systemConfiguration",
1400: "applicationErrors"
};
for (var key in arr) {
if( typeof objects[arr[key]] != 'undefined' ) console.log(objects[arr[key]]);
}
答案 1 :(得分:2)
您可以使用underscore.js选择方法。
var arr = ["1398","1392","1390"];
var objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
console.log(_.pick(objects,arr))
//For Values
console.log(_.chain(objects).pick(arr).values().value());
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
答案 2 :(得分:1)
你可以循环你的数组,然后检查当前索引是否在你的对象中:
var objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
var arr = ["1398","1392","1390"];
arr.forEach(function(entry) {
if(entry in objects) console.log(objects[entry]);
});
&#13;
答案 3 :(得分:1)
我该怎么做?
通过传递map
函数来使用callback
方法。
var arr = ["1398", "1392", "1390"];
var objects = {
1384: "registerItem",
1386: "createGroups",
1388: "vipNumbers",
1390: "targetNumbers",
1392: "ignoredNumbers",
1394: "globalConstants",
1396: "Users",
1398: "systemConfiguration",
1400: "applicationErrors"
};
console.log(arr.map(a => objects[a]));
&#13;
答案 4 :(得分:1)
我需要找到键在数组中的对象的所有值。
在ES5环境中,我可能会使用forEach
和push
,使用in
或hasOwnProperty
进行检查(具体取决于您是否要查看原型)查看对象是否有密钥:
var arr = ["1398","1392","1390"];
var objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
var values = [];
arr.forEach(function(key) {
if (key in objects) {
values.push(objects[key]);
}
});
console.log(values);
&#13;
在ES2015 +环境中,我可能会使用for-of
代替forEach
:
const arr = ["1398","1392","1390"];
const objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
const values = [];
for (const key of arr) {
if (key in objects) {
values.push(objects[key]);
}
}
console.log(values);
&#13;
最后,如果已经知道该对象包含属性而根本不需要检查,则可以使用map
:
var arr = ["1398","1392","1390"];
var objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
var values = arr.map(function(key) {
return objects[key];
});
console.log(values);
&#13;
......在ES2015 +中可以使用简洁的箭头功能:
const arr = ["1398","1392","1390"];
const objects = { 1384: "registerItem", 1386: "createGroups", 1388: "vipNumbers", 1390: "targetNumbers", 1392: "ignoredNumbers", 1394: "globalConstants", 1396: "Users", 1398: "systemConfiguration", 1400: "applicationErrors" };
const values = arr.map(key => objects[key]);
console.log(values);
&#13;
答案 5 :(得分:1)
尝试减少数组,并创建一个新对象{}
并为对象中存在的那些键取key,value
对,或者创建一个新数组[]
并保持推送对象中存在的那些键的值。取决于你的要求。
arr.reduce(function(init, key) {
if(objects[key]) { init[key] = objects[key]; };
return init;
}, {});
OR
arr.reduce(function(init, key) {
if(objects[key]) { init.push(objects[key]); };
return init;
}, []);
并使用underscore
或lodash
更好地从数组端进行迭代并将对象用作map,而不是从对象侧进行迭代,这里你去:
_(arr).map(k=>objects[k]).compact().value()