0

I have an array of objects, an example of a few of them being

[
    {
        "lang_code":        "eng",
        "site_language":    "1",
        "name":         "English"
    },
    {
        "lang_code":        "afr",
        "site_language":    "1",
        "name":         "Afrikaans"
    },
    {
        "lang_code":        "ale",
        "site_language":    "0",
        "name":         "Aleut"
   },
]

I want to be able to search the whole array for a specific lang_code, let's say I use eng. I want to search the whole array for eng. If it's there, I want it to return English, if not, I want it to return invalid. Any ideas on this?

1
  • for loop, filter()/map() combo ... plenty of options Commented Mar 26, 2015 at 12:31

5 Answers 5

2

A generic and more flexible version of the findById function above:

// array = [{key:value},{key:value}]
function objectFindByKey(array, key, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][key] === value) {
            return array[i];
        }
    }
    return null;
}

var array = your array;
var result_obj = objectFindByKey(array, 'lang_code', 'eng');

Click here for demo

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

Comments

2

You could use filter:

function findLang(arr, code) {
  var filtered = arr.filter(function (el) {
    return el.lang_code === code;
  });

  // filter returns an array of objects that match the criteria
  // if the array is not empty return the language,
  // otherwise return 'invalid'
  return filtered.length > 0 ? filtered[0].name : 'invalid';
}

findLang(arr, 'eng'); // English

DEMO

If you wanted to add map into the mix instead of using that ternary operation (but which would most likely be slower and doesn't really provide any additional benefit):

function findLang(arr, code) {
  return arr.filter(function (el) {
    return el.lang_code === code;
  }).map(function (el) {
    return el.name;
  })[0] || 'invalid';
}

DEMO

Comments

1

How about a for loop. Something like:

function find_lang(input_arr, lang_code) {
   for(var i = 0; i < input_arr.length; i++) {
      var o = input_arr[i];
      if( o.lang_code === lang_code ) {
         return o.name;
      }
   }

   return "invalid";
}

Comments

1

Underscore.js has some useful helpers for this kind of thing: http://underscorejs.org/

E.g Find:

Looks through each value in the list, returning the first one that passes a truth test (predicate), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
    => 2

Where:

Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in properties.

_.where(listOfPlays, {author: "Shakespeare", year: 1611});
=> [{title: "Cymbeline", author: "Shakespeare", year: 1611},
    {title: "The Tempest", author: "Shakespeare", year: 1611}]

Comments

0

You could do it this way:

// configure your parameters
function checkArr(){
    for (var i = 0; i < x.length; i++){
        if (x[i].lang_code == "eng")
            return "English";
    }
    return "invalid";
}

var x = [
{
    "lang_code":        "eng",
    "site_language":    "1",
    "name":         "English"
},
{
    "lang_code":        "afr",
    "site_language":    "1",
    "name":         "Afrikaans"
},
{
    "lang_code":        "ale",
    "site_language":    "0",
    "name":         "Aleut"
}
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.