-2

I want to make an array so that it contains some identity name and for each of those names there is another array associated. My approach is like,

for (var i = 0; i < 10; i++) {
    for (var x = 0; x < 5; x++) {
        var data = someServiceCall(i, x);
        var identityName = i + '-' + x;
        myArray[identityName] = data;
    }
}

after executing this i get something like,

[1-1: Array(8), 1-2: Array(10), 1-3: Array(10), 1-4: Array(10),.. etc]

the next time I call this function I need to check whether 1-1 exists and if yes I need to get the list related to 1-1. How can I do this..? if 1-1 is not in the myArray I will call some other function.

dannemp
  • 919
  • 1
  • 14
  • 32
user2837480
  • 349
  • 2
  • 18

5 Answers5

2

To check if the element having the 1-1 key exists just do:

if("1-1" in myArray)

Then to access the array associated with 1-1 use:

myArray["1-1"]

dannemp
  • 919
  • 1
  • 14
  • 32
  • 1
    Another option is `myArray.hasOwnProperty('1-1')` which doesn't look at inherited properties like `in` does – Ruan Mendes Jul 13 '17 at 12:26
  • instead of doing this `if("1-1" in myArray)` just try to access given key as `if(myArray[key])`. For detail see my solution. – Zeeshan Jul 13 '17 at 12:31
  • @Zeshan That's not a very good solution, what if the value is `false`, an empty string, or `0`? If you want to know if a key exists, check if a key exists, not if a value exists – Ruan Mendes Jul 13 '17 at 12:42
  • @JuanMendes you are right but if you go through the problem in question each key will be an `array` not `string`, `false`, or ` 0`. However, I understand I understand implications of cases and by the way you have a keen eye for the detail :) Thanks. – Zeeshan Jul 13 '17 at 13:11
  • @Zeshan If you add a caveat that the solution only applies to this case because of the assumption that the values are objects, that is fine. I just like to point out problems so others are aware of it before copy/pasting into their use case, remember that Stack Overflow questions/answers are not just for the benefit of the OP. – Ruan Mendes Jul 13 '17 at 15:30
1

Try this. It inserts an object containing the identity name and data in each array element.

var myArray = [];
for (var i = 0; i < 10; i++) {
    for (var x = 0; x < 5; x++) {
        var data = someServiceCall(i, x);
        var identityName = i + '-' + x;
        var objectInArr = {
            'identityName': identityName,
            'data' : data
        };
        myArray.push(objectInArr);
    };
};
Ezis
  • 73
  • 8
1

try like this

myArray["1-1"] != undefined
1

Check if key exists in your array or not and act accordingly. Following is a working code snippet

var myArray = []; 
for (var i = 0; i < 10; i++) {
    for (var x = 0; x < 5; x++) {        
        var identityName = i + '-' + x;
        myArray[identityName] = [1, 2];
    }
}
var key = "0-0";
if(myArray[key])
  console.log(myArray[key]);
Zeeshan
  • 619
  • 4
  • 16
0

you can check your array lenght and if the array is empty you will know that you need to call another action as you say. something like below might work for you

    if (myArray.length === 0) {
      //Call another function}
    else {
      //Somthing else}