I have the following line of code which returns me one or more arrays depending on the checkbox that is clicked.
selected.forEach(langsel => {
let filtered = person.filter(pers => pers.language == langsel);
})
selected and I do not report the other variables for simplicity in reading.
I have a list of checkboxes where each refers to a particular language. So every time a checkbox is clicked I want the people who speak that language to be returned to me; this must be returned in an array of objects.
catsel refers to the checkbox that is selected (being a checkbox, more than one can also be selected). So for each language that is selected it returns me the array of objects in the filtered variable.
For example, if I select the English language through the checkbox, I get:
[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];
if together with the checkbox selected previously, therefore English, I also select the checkbox relating to the Spanish language, filtered returns me:
[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];
[{id: "15", name: "Alex Atres", language: "Spanish"}, {id: "1", name: "Mark Sertoj", language: "Spanish"}, id: "12", name: "Martha Forrest", language: "Spanish"];
filtered in the latter case returns me two separate arrays.
I want them to be merged into an array.
Can anyone kindly help me?
I concat the array by using spread operator.
let people = [];
selected.forEach(langsel => {
let filteredPerson = person.filter(pers => pers.language == langsel);
people = [...people, ...filteredPerson];
})