Bilal Zafar
Bilal Zafar

Reputation: 457

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]);
    }
}

Upvotes: 2

Views: 414

Answers (6)

Koushik Chatterjee
Koushik Chatterjee

Reputation: 4175

Try reducing the array, and either create a new object {} and take the key,value pairs for those keys which are exists in the object, or create a new array [] and keep pushing the values for those keys which are exists in the object. depends on your requirement.

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;
}, []);

and using underscore or lodash better to iterate from the array side and use the object as map, instead of iterating from object side, here you go:

_(arr).map(k=>objects[k]).compact().value()

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074168

I need to find all the values from objects whose key is in array.

In an ES5 environment, I'd probably use forEach and push, checking with in or hasOwnProperty (depending on whether you want to check the prototype) to see if the object had the key:

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);

In an ES2015+ environment, I'd probably use for-of instead 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);

Finally, if you already know the object contains the properties and don't need a check at all, you could use 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);

...which in ES2015+ could be use concise arrow function:

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);

Upvotes: 1

Eddie
Eddie

Reputation: 26844

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]]);
}

Upvotes: 3

Kishore Kumar
Kishore Kumar

Reputation: 12874

You can use underscore.js pick method.

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());
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Upvotes: 2

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

How can i do this ?

Just use map method by passing a callback function.

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]));

Upvotes: 1

Amani Ben Azzouz
Amani Ben Azzouz

Reputation: 2565

you can loop your array and then check if the current index is in your object:

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]);
});

Upvotes: 1

Related Questions