Jump to content
ShadowRanger24

Finding nested array from value

Recommended Posts

Hi everyone.

 

So I'm having a bit of trouble finding a way to get a nested array via a certain value. Here's what the array looks like:

_array = [
    ["Key 1", [0, 0, 0], [0, 0, 0]],
    ["Key 2", [0, 0, 0], [0, 0, 0]],
    ["Key 3", [0, 0, 0], [0, 0, 0]] 
];

I want to be able to get the array containing the value just by searching for the key. So for example I would like to get "["Key 1", [0, 0, 0], [0, 0, 0]]" just by searching for "Key 1".

 

Does anyone know how I can do this? Perhaps there's an easier way I could approach this instead? Thanks in advance.

Share this post


Link to post
Share on other sites

Hi everyone.

 

So I'm having a bit of trouble finding a way to get a nested array via a certain value. Here's what the array looks like:

_array = [
    ["Key 1", [0, 0, 0], [0, 0, 0]],
    ["Key 2", [0, 0, 0], [0, 0, 0]],
    ["Key 3", [0, 0, 0], [0, 0, 0]] 
];

I want to be able to get the array containing the value just by searching for the key. So for example I would like to get "["Key 1", [0, 0, 0], [0, 0, 0]]" just by searching for "Key 1".

 

Does anyone know how I can do this? Perhaps there's an easier way I could approach this instead? Thanks in advance.

_array = [
    ["Key 1", [0, 0, 0], [0, 0, 0]],
    ["Key 2", [0, 0, 0], [0, 0, 0]],
    ["Key 3", [0, 0, 0], [0, 0, 0]] 
];

_keyStr = "Key 1";
_key = _array select {(_x select 0) isEqualTo _keyStr} select 0;
// Returns ["Key 1", [0, 0, 0], [0, 0, 0]]
  • Like 1

Share this post


Link to post
Share on other sites
_array = [
    ["Key 1", [0, 0, 0], [0, 0, 0]],
    ["Key 2", [0, 0, 0], [0, 0, 0]],
    ["Key 3", [0, 0, 0], [0, 0, 0]] 
];

_searchKey = "Key 1";

_matchedArray = [];

{
	_keyArray = _x;
	if ((_keyArray select 0) == _searchKey) exitWith
	{
		{
			_matchedArray pushBack _x;
		} forEach _keyArray;
	};
} forEach _array;
_matchedArray;

Untested, but should work....but give you a concept.

 

Forgot about the select command, use hallyGs post :D.

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×