6

I have a JSON string as:

  var Str="[{ 'label': 'Month'},{ label: 'within'},{ label: 'From'},
         { label: 'Where'},]";

I converted it into an objects by eval:

      var tagString = eval(Str); 

I want to get the index of month without a loop.

Is there a better way to get the index of an object in an array without using loops?

Thanks in advance!

9
  • 4
    That's not JSON. But you should use JSON.parse anyway, not eval... Commented May 15, 2013 at 7:21
  • I don't think so, with the given datat structur you need to loop Commented May 15, 2013 at 7:21
  • if i use JSON.parse it will give error like this JSON.parse: expected property name or '}' Commented May 15, 2013 at 7:23
  • 2
    Because it's not proper JSON format. It should be all double quotes and I don't think trailing commas are valid either. Commented May 15, 2013 at 7:23
  • 1
    @vdua: "without using loops" and "without iterating" are not synonymous - built in functions allow you to express things without using for or while, as in my answer. Commented May 15, 2013 at 7:41

4 Answers 4

27

Don't parse json with eval! Use JSON.parse. Array.map is a good alternative to looping here:

var str = '[{ "label": "Month"}, { "label": "within"}, { "label": "From"}, { "label": "Where"}]';
var data = JSON.parse(str);
var index = data.map(function(d) { return d['label']; }).indexOf('Month')

jsFiddle

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

3 Comments

But your solution is iterating the array.
@vdua: Yes, that's the only way to search a list.
That's correct, I misunderstood the question and assumed he didn't want to iterate the array. My Bad. Thanks anyways
14

If those are all labels, you could change the structure like this, so it's "An array of labels" which, in my opinion, would be more proper.

var Str = '["Month","within","From","Where"]';

Then parse it them with JSON.parse, or since you are using jQuery, $.parseJSON to get it to work on more browsers:

var labels = JSON.parse(Str);

labels should now be an array, which you can use Array.indexOf.

var index = labels.indexOf('Month');

It's ES5 and most modern browsers support it. For older browsers, you need a polyfill which unfortunately... also uses a loop.

3 Comments

if i use var index = labels.indexOf('Month'); it is return -1
@amol My answer suggests you change the structure of the data.
@Eric I cant change this data structure because I am using the visual search plug-in & it needs json object as I given in the question & fiddle
2

Note: if you want to get a specific item in JSON by its value, I came across this answer and thought about sharing it.

You can use Array.splice()

First as @elclanrs and @Eric mentioned use JSON.parse();

var Str = '[{ "label": "Month"}, { "label": "within"}, { "label": "From"}, { "label": "Where"}]';

var item = JSON.parse(Str);

item = item.splice({label:"Month"},1);

and Its ES5.

See the snippet code here

var Str = '[{ "label": "Month"}, { "label": "within"}, { "label": "From"}, { "label": "Where"}]';
var item = JSON.parse(Str);

item = item.splice({label:"Month"},1);

console.log(item);

Comments

0

Use Array.findIndex() when searching by complex condition

Array.findIndex()

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.