1

In JavaScript, how is the best way to code a way to check if a value exists in an array of data?

Here is what I currently have:

var html = [];

html.push({
    key: "/1/1.html", 
    value:  "html 1 data"
});

if(doesKeyExist(html, "/1/1.html"))
{
    alert(getValue(html, "/1/1.html"));
}

function doesKeyExist(arr, key)
{
    $.each(arr, function (index, data) {
        if(data.key === key)
            return true;
    });
    return false;
}

function getValue(arr, key)
{
    $.each(arr, function (index, data) {
        if(data.key === key)
            return data.value;
    });
}

The above code does not alert the value of "html 1 data", and no error is shown in the console.

May I please have some help with the above code?

Thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Simon
  • 7,991
  • 21
  • 83
  • 163

3 Answers3

1

The issue is because your return statements inside the $.each blocks are returning from the anonymous functions not from the doesKeyExist() and getValue() functions. You need to change your logic slightly:

var html = [];

html.push({
    key: "/1/1.html",
    value: "html 1 data"
});

if (doesKeyExist(html, "/1/1.html")) {
    alert(getValue(html, "/1/1.html"));
}

function doesKeyExist(arr, key) {
    var result = false;
    $.each(arr, function (index, data) {
        if (data.key === key) {
            result = true;
            return; // stop the loop
        }
    });
    return result;
}

function getValue(arr, key) {
    var result = '';
    $.each(arr, function (index, data) {
        if (data.key === key) {
            result = data.value;
            return; // stop the loop
        }
    });
    return result;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

The issue is because you are returning a value in inside the each anonymous function scope, this not return the value to the function so it is always returning false.

So you can use a function variable and return it.

Code:

var html = [];

html.push({
    key: "/1/1.html", 
    value:  "html 1 data"
});

html.push({
    key: "/1/2.html", 
    value:  "html 2 data"
});

if(doesKeyExist(html, "/1/1.html"))
{
    alert(getValue(html, "/1/1.html"));
}

function doesKeyExist(arr, key)
{
    var exist= false;

    $.each(arr, function (index, data) {
        if(data.key === key) {
            exist=true;      
            return false;
        }
    });
    return exist;
}

function getValue(arr, key)
{
    var value= '';

    $.each(arr, function (index, data) {
        if(data.key === key) {
            value=data.value;      
            return false;
        }
    });
    return value;
}

Demo: http://jsfiddle.net/IrvinDominin/v989gmxc/2/

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
0

you said you just want to check if a value exist in array check this out http://jsfiddle.net/elviz/nrfm943e/

     var html = [];

        html.push({
           key: "/1/1.html", 
           value:  "html 1 data"
         });

      $.each(html, function (index, data) {
        if(data.value){
            alert(data.value);
          }

         });
knives22
  • 303
  • 1
  • 2
  • 13