Hello everyone, first of all good day and thanks for your answers and opinions, I have the following problem I want to find in an array that contains other arrays the element that has been modified for it I have done the following I save the elements that I receive in two different variables in one of them I make the modifications and with the other array the idea is to compare it to know which element has been modified and to obtain it I have the following structure. I save the data that comes from the server in Data and modifiableData. I save the data that comes from the server in Data and modifiableData
data = [
[1, 'Darinel', 'Cigarroa'],
[2, 'Isis', 'Oriana'],
[3, 'Manuel', 'Arcos']
];
modifiableData = [
[1, 'Darinel', 'Cigarroa'],
[2, 'Isis', 'Oriana'],
[3, 'Manuel', 'Arcos']
];
Suppose that in modifiableData I have changed the first name to Daro, it would be as follows:
modifiableData = [
[1, 'Daro', 'Cigarroa'],
[2, 'Isis', 'Oriana'],
[3, 'Manuel', 'Arcos']
];
I want to find that modified array and fetch only that array to have this result:
[1, 'Daro', 'Cigarroa'],
It is worth mentioning that I am receiving the data that I am modifying following our 'Daro' example, so I suppose that there must be more than one solution. I was solving it by searching for its id, which would be the first value of the arrays, but this can also be modified and my solution fails now i am trying as follows but no success.
data = [
[1, 'Darinel', 'Cigarroa'],
[2, 'Isis', 'Oriana'],
[3, 'Manuel', 'Arcos']
];
modifiableData = [
[1, 'Daro', 'Cigarroa'],
[2, 'Isis', 'Oriana'],
[3, 'Manuel', 'Arcos']
];
const result = modifiableData.filter((element) => element === 'Daro' );
console.log(result);
Note that you are trying to filter data - which are arrays - into another array. So what you
filter
're doing is iterating over the parent of thearrays
.Change it to this, and it will work for you
What we are doing, is that within the parent, for each iteration, ask each child or array via find , if there is such a match.
If you have doubts, tell us.