EchoFool Posted March 20, 2012 Share Posted March 20, 2012 Hey I have an array which carries id's + 4 numbers assigned to each id. I'm trying to find a way to use if statement to get the correct id by checking which of the 4 numbers goes with the check. This is the structure: var listObj = { id: uid, data: [ abposx, abposy, width, height ] }; So lets say im trying to find the "id" which greater than the width & height and its abposx and abposy and greater than what im checking... The issue is how do i get the id from such a check =/ its confusing? Quote Link to comment https://forums.phpfreaks.com/topic/259310-get-correct-row-from-an-array/ Share on other sites More sharing options...
requinix Posted March 20, 2012 Share Posted March 20, 2012 Use a loop. Look through everything and return the current item's ID once you find a match. If there could be more than one you scan the whole array and track all the matching IDs. Quote Link to comment https://forums.phpfreaks.com/topic/259310-get-correct-row-from-an-array/#findComment-1329295 Share on other sites More sharing options...
EchoFool Posted March 20, 2012 Author Share Posted March 20, 2012 Is looping the entire array the only way? As it sounds quite intensive. Also how can you loop an array in such a way to check if the values are between the values like: Logic code: get id where x is > data[0] && y > data[1] && x < data[2] && y < data[3] Quote Link to comment https://forums.phpfreaks.com/topic/259310-get-correct-row-from-an-array/#findComment-1329300 Share on other sites More sharing options...
requinix Posted March 20, 2012 Share Posted March 20, 2012 How else are you going to find what you want unless you're prepared to search the entire array? The only way was if the array was sorted or otherwise preprocessed, but to do that from scratch you'd still have to look at everything. Your logic is great but plain JavaScript doesn't work like that. As you still haven't said whether you want just one id or all the matching ids, for one the logic is looking at each item in the array, if x > data[0] and y > data[1] and x and for many [code]start with an empty list. looking at each item in the array, if x > data[0] and such then add the id to the list. after searching the array, return the list Quote Link to comment https://forums.phpfreaks.com/topic/259310-get-correct-row-from-an-array/#findComment-1329315 Share on other sites More sharing options...
EchoFool Posted March 20, 2012 Author Share Posted March 20, 2012 Ah i see the latter is my best choice! Thanks for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/259310-get-correct-row-from-an-array/#findComment-1329317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.