-1

I have an array of objects(about 10 objects) with 2 properties having their respective values.

I want to check if another value of the same property exists or not.

How can i do that ?

For example

array = [{'family':'Roboto', 'type': 'Google'}, ......]

I want to search 'Roboto'. How do i do ?

EDIT:-
I am using Google Fonts API to get all the Google fonts into a single array. I have succeeded in that. Codepen. I store an array with the 'family' and 'url' of each font.

Now i want the user to search for a font. If the font is found, do something, else "Font not found"

How can i acheive this ?


Note:- A solution provided would be helpful.

  • Use [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Casimir et Hippolyte May 18 '17 at 09:16
  • Use [`Array.prototype.some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – Bergi May 18 '17 at 09:17

2 Answers2

0

If you want to find all appearance use: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

If you want to find the first appearance use: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find

schaffioverflow
  • 510
  • 3
  • 14
  • Neither of them does "*check if another value of the same property exists or not*" – Bergi May 18 '17 at 09:18
  • I don't understand you? For example if he filter and his callback return an entry another value is found with this property. Or do I understand something wrong? – schaffioverflow May 18 '17 at 09:25
  • "check" and "or not" implies that he's looking for a boolean result value – Bergi May 18 '17 at 09:27
  • Ok sorry, I didn't know of the existence of your function. :) I would just checking the length of the resulting array. But yours are nice to know. Thanks for that! – schaffioverflow May 18 '17 at 09:45
-1

That object is in the array, so array[0].family will return Roboto. or Just Declare the array like this: array = Object{..}. maybe I think array = array[0]; console.log(array.familly) will works too.