I want to get an element of an array of arays that matches a data, in this case the first data of my array, which refers to the id of each array.
var data =
[
[1, 'darinel', 24, '9a3853'],
[2, 'oscar', 19, 'ecf03c'],
[3, 'Alexander', 15, 'aab418']
];
I receive a data, in this case the id, through a form; I have the following data:
var data =
[
[1, 'darinel', 24, '9a3853'],
[2, 'oscar', 19, 'ecf03c'],
[3, 'Alexander', 15, 'aab418']
];
var id = 1;
var people = data.filter((element) => element === id);
console.log(people);
But this returns me empty; I am thinking of going through a FOR although I am not sure how to do it. I forgot to mention that the array of arrays can vary and can have different dimensions.
The filter would return a new array with a single element inside that is already an array. It would be like this, where 0 would be the index of the array that you are iterating into the parent array.
This would return an array with the array traversed inside, that is: people would be equal to: [[1, "darinel", 24, "9a3853"]], and to access that you would enter this array at position 0 and then the position of each data, eg: 0 the id, 1 the name, 2 the other. Like this: people[0][1] - This would bring you the name.
Maybe this works for you.
Another option is instead of generating a string with everything concatenated to use an object, it would look like this:
This returns an object like this: {id: 1, name: "darinel", thirdData: 24} So now you can go into result.id, or result.name or result.thirdData(I didn't know what it was, I guess color per hex of 6 digits)
If you change the value of the id variable, it will bring you the person with that id, I deposited them in a string, but you can do with the data what you want in that same place.
I hope this little code will help you or give you some idea that you can implement in your code.