Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find values from key,val object by giving its key

I am trying to find values from KEY,VALUE pair of object by giving the KEY. The key is in array.

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" };

I need to find all the values from objects whose key is in array. How can i do this ? So far I did this :

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]);
    }
}
like image 913
Bilal Zafar Avatar asked Jan 20 '26 15:01

Bilal Zafar


1 Answers

You can find the value by objects[ arr[ 0 ] ] will return systemConfiguration

Where 0, 1 and 2 are the key of arr


You can loop to find all. Like:

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]]);
}
like image 131
Eddie Avatar answered Jan 23 '26 03:01

Eddie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!