0

I have got a problem by searching JavaScript array by variable.

var category = $(this).data('ord');

Data category contains an array i.e: {"name":"John", "subsite":"example.com"}

I would like to search this array by variable called filter that contains an filter.

I was trying to do var search = jQuery.inArray( filter, ord ); but this return undefined

I also was trying to do it in this way: var ord = $(this).data('ord').category; this is not working too.

When i'm trying to do var category = JSON.parse($(this).data('ord'));

Console returns an error: SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data var order = JSON.parse($(this).data('ord'));

Arrow is pointing on JSON pharse.

Can somebody explain to me how to do that correctly?

EDIT:

var ord = parseInt($(this).data('ord')[filter]);

Following code is looking for index by variable.

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
  • Maybe this can help you out: http://stackoverflow.com/a/4992429/4730201 – Ricardo Pontual Sep 16 '16 at 10:13
  • 2
    `I would like to search this array` - you can't, because it's not an array – Jaromanda X Sep 16 '16 at 10:19
  • `data-ord='{"all": "43","artist": "44","composer": "43"}'` Why this is not an array –  Sep 16 '16 at 10:20
  • `Arrow is pointing on JSON pharse` - because you are trying to JSON.parse something that is not JSON ... JSON is a string notation - you have a javascript object, so no need to parse it as a JSON string – Jaromanda X Sep 16 '16 at 10:20
  • so what should i do? i stuck in this problem –  Sep 16 '16 at 10:21
  • `data-ord='{"all": "43","artist": "44","composer": "43"}'` - to the right of the = you have a JSON string, which would need to be parsed - however, what are you trying to say with that piece of incorrect "code" – Jaromanda X Sep 16 '16 at 10:22
  • 1
    `so what should i do?` - step 1, understand what an Array is, and that you don't have one, step 2, write code to search through an objects properties, step 3 - profit – Jaromanda X Sep 16 '16 at 10:23
  • i resolved my problem. Edit in question above –  Sep 16 '16 at 10:25
  • Thanks for your tips. It is better than ready to go code –  Sep 16 '16 at 10:26

0 Answers0