-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.

4
  • Can you post code which you have tried? Commented Jul 13, 2017 at 12:21
  • i get something like - can you tell exactly what you are getting? Commented Jul 13, 2017 at 12:23
  • 1
    First, if you are keying by a string, it's not an array, it's an object, being used as a map. Answer if ('1-1' in myArray) or if (myArray.hasOwnProperty('1-1')) Commented Jul 13, 2017 at 12:25
  • @HassanImam if (myArray.includes(1-1)) { mydata = myArray[1-1]; } Commented Jul 13, 2017 at 12:33

5 Answers 5

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"]

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

5 Comments

Another option is myArray.hasOwnProperty('1-1') which doesn't look at inherited properties like in does
instead of doing this if("1-1" in myArray) just try to access given key as if(myArray[key]). For detail see my solution.
@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
@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.
@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.
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);
    };
};

Comments

1

try like this

myArray["1-1"] != undefined

Comments

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]);

Comments

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}

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.