1

Having a JSON string (shortened):

{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}

I would like to be able to call a function that returns the "long" value based on the "short" value:

like:

var test= 'Say '+get_value("_YES");

How can I do this?

Tried :

function f_lang(short_string) {
    var obj = json_string;
    var arr = [];
    json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
    arr = $.parseJSON(json); //convert to javascript array

    return arr['line_array'][short_string];
}

with no luck

5 Answers 5

1

Use Array#find to find the object that contains the short value. Note that Array#find is not supported by IE. So if you need IE support and/or you're making lots of conversion like this, you should go with the dictionary approach.

var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';

var terms = JSON.parse(str);

function get_value(short) {
   var term = terms.line_array.find(function(o) {
     return o.short === short;
   });
  
  //in case the term isn't found, we'll prevent term.long from throwing an error
  return term && term.long;
}

var result = get_value('_YES');

console.log(result);

Using a dictionary object

Creating a dictionary with Array#reduce, and using it:

var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';

var terms = JSON.parse(str);

var termsDictionary = terms.line_array.reduce(function(d, t) {
  d[t.short] = t.long;
  return d;
}, Object.create(null));

function get_value(short) {
  return termsDictionary[short]; // you can use this expression without the function of course
}

var result = get_value('_YES');

console.log(result);

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

5 Comments

Gives TypeError: termsDictionary is undefined on the dictionary example
Using it. * var terms = str; var termsDictionary = terms.line_array.reduce(function(d, t) { d[t.short] = t.long; return d; }, Object.create(null)); function get_value(short) { return termsDictionary[short]; // you can use this expression without the function of course } console.log(get_value("_YES")); *
gives TypeError: terms.line_array is undefined[Learn More]
var terms = JSON.parse(str) - it's a json that you need to parse. I'll update the examples.
Changed var terms = str; to var terms = JSON.parse(str); and now it works.. ;-)
0

With a proper parsed JSON, you could use Array#find, an ES6 feature.

function getValue(short_string) {
    return (object.line_array.find(a => a.short === short_string) || {}).long;
}

var json_string = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}',
    object = JSON.parse(json_string),
    test= 'Say '+getValue("_YES");

console.log(test);

ES5 with Array#some

function getValue(short_string) {
    var value;
    object.line_array.some(function (a) { 
        if (a.short === short_string) {
            value = a.long;
            return true;
        }
    });
    return value;
}

var json_string = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}',
    object = JSON.parse(json_string),
    test= 'Say '+getValue("_YES");

console.log(test);

Comments

0

Another option:

function f_lang(short_string) {
    var obj = {"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]};
    for (var i = 0; i < obj['line_array'].length; i++) {
      if (obj['line_array'][i]['short'] == short_string) {
        return obj['line_array'][i]['long'];
      };
    }
}

console.log('Say ' + f_lang("_YES"));

Comments

0

You can use Array.Filter

var haystack = {"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}

console.log(haystack.line_array.filter(e => e.short === '_YES').long)

Comments

0

please try below code

<script>
var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';

var terms = JSON.parse(str);

function get_value(short) {
   return terms.line_array.filter(function(o) {
        return o.short == short
   });

  //in case the term isn't found, we'll prevent term.long from throwing an error
}

var result = get_value('_YES');

console.log(result);
</script>

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.