1

I have written one array as below

const OBJECT = {
    '3': 'History And Social Sciences',
    '5': 'Humanities',
    '8': 'Global Studies And Social Impact',
    '10': 'Sanskrit'
};

var rou=[3,5,8,10];

I want to fetch the field values when keys are matching with values. Does anyone have any idea on it?

7 Answers 7

2

map over rou and get the value from OBJECT of that key:

const OBJECT = {
  '3': 'History And Social Sciences',
  '5': 'Humanities',
  '8': 'Global Studies And Social Impact',
  '10': 'Sanskrit',

};

var rou = [3, 5, 8, 10];
var values = rou.map(k => OBJECT[k]);
console.log(values);

Sign up to request clarification or add additional context in comments.

1 Comment

There is a problem if array contains a value that is not present inside the object it will console undefined.
0

rou[0] is 3 , rou[1] is 5 and so on. What you want is to check if rou[0] that has the value 3, is key to you dictionary.

Comments

0

You can access the value from the object like this

var value = OBJECT.rou[0] 

Iterate 'rou' array and for each value of it find the corresponding value in the OBJECT

Comments

0
   Iterator<String> it = jsonObject.keys(); 
   List<Integer> list=new ArrayList();
   while(it.hasNext()){
   int key = it.next(); 
   list.add(key);
   }

2 Comments

The question is asked for Javascript and not Java
Please provide some explanation - what makes you think that this code solves the given problem?
0

With ramdajs' pick, it's easier.

R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}

If you're interested, check its implementation

Comments

0

Loop over the object properties and compare if the key is found in arr by indexOf if found get the value of that key.

const OBJECT = {'3': 'History And Social Sciences','5': 'Humanities','8': 'Global Studies And Social Impact','10': 'Sanskrit','a': 'abc'};

var rou = [3, 5, 8, 10];

for (var property in OBJECT) {
  if (OBJECT.hasOwnProperty(property) && rou.indexOf(parseInt(property)) != -1) {

    console.log(OBJECT[property]);
  }
}

You can also do it by iterating the array and if the element of array is present in the object get the value of that object

    const OBJECT = {'3': 'History And Social Sciences','5': 'Humanities','8': 'Global Studies And Social Impact','10': 'Sanskrit','a': 'abc'};

    var rou = [3, 5, 8, 10];

    rou.forEach(function(element) {
      if (OBJECT[element] != undefined) {
        console.log(OBJECT[element]);
      }
    });

Comments

0

const OBJECT = {
    '3': 'History And Social Sciences',
    '5': 'Humanities',
    '8': 'Global Studies And Social Impact',
    '10': 'Sanskrit'
};

var rou=[3,5,8,10];

var res = Object.keys(OBJECT).reduce((acc, cur) => rou.indexOf(parseInt(cur)) !== -1 ? (acc.push(OBJECT[cur]), acc) : acc, [])

console.log(res)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.