-2

I need to find the third element of a dictionary array knowing the first two values. For example:

    var dict = [{name: "A", game: "X", number: 5},
               {name: "B", game: "X", number: 3},
               {name: "A", game: "Y", number: 1},
               {name: "C", game: "Z", number: 2}]

So if I find name: "A" and game: "X" this will return number: 5 or just 5. And if it is posible, getting the index in the array of that element, for example in that case the index would be 0 because that is in dict[0]

Yamika Izumi
  • 19
  • 1
  • 7
  • 1
    Did you try anything ? What part was a problem ? – Denys Séguret May 01 '18 at 13:53
  • 1
    `var dict = arr.find(x => x.name === 'A' && x.game === 'X').number`. Note that JavaScript doesn't call them dictionaries. – Jared Smith May 01 '18 at 13:54
  • It's a duplicate of https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property – Denys Séguret May 01 '18 at 13:57
  • Possible duplicate of [Get JavaScript object from array of objects by value of property](https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property) – Michael Hancock May 01 '18 at 14:03

3 Answers3

0

You can use find() method if you want to find specific object from array and return it.

 var dict = [{name: "A", game: "X", number: 5}, {name: "B", game: "X", number: 3}, {name: "A", game: "Y", number: 1}, {name: "C", game: "Z", number: 2}]
               
const obj = dict.find(({name, game}) => name == 'A' && game == 'X');
if(obj) console.log(obj.number)

If you want to get index you can use findIndex method instead which is going to return index if element is found or -1 if not.

 var dict = [{name: "A", game: "X", number: 5}, {name: "B", game: "X", number: 3}, {name: "A", game: "Y", number: 1}, {name: "C", game: "Z", number: 2}]
               
const index = dict.findIndex(({name, game}) => name == 'A' && game == 'X');
console.log(index)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

ES6

You could use find() method of array

The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

DEMO

const arr = [{name: "A", game: "X", number: 5},
{name: "B", game: "X", number: 3},
{name: "A", game: "Y", number: 1},
{name: "C", game: "Z", number: 2}];
               
let result = arr.find( ({ name, game }) => name=='A' && game=='X');               
               
if(result){
  console.log(result.number)
}
.as-console-wrapper {max-height: 100% !important;top: 0;}
Community
  • 1
  • 1
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
0

If I don't misunderstood your question. This is what you can try with find() and findIndex()

var dict = [{name: "A", game: "X", number: 5},
               {name: "B", game: "X", number: 3},
               {name: "A", game: "Y", number: 1},
               {name: "C", game: "Z", number: 2}];

//find other property value based on some property value
var find = dict.find(elm => elm.name ==='A' && elm.game==='X').number;

//find the index based on property value
var findIndex = dict.findIndex(elm => elm.name==='A' && elm.game==='X');

console.log(find);
console.log(findIndex);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103