First of all, give thanks for reading my question and try to help me and apologize for my English.
My problem is that I would like implement a nice solution, using less arrays inside arrays.
I have n arrays with n objects, each object has two properties: name and type. I would like to know how get objects that are in all arrays repeated.
Here is my code:
let arraysNoEmpties = [...res].filter(array => array.length > 0);
let attributes = arraysNoEmpties[0].map(p => {
return { name: p.name, type: p.type };
});
let items = getAtributeInAllArrays(attributes, arraysNoEmpties);
console.log('items: ', items);
const getAtributeInAllArrays = (properties, list) => {
let items = [];
for (let i = 0; i < properties.length; i++) {
const property = properties[i];
let numTimes = 0;
for (let j = 0; j< list.length; j++) {
const array = list[j];
let propsOfarray = array.map(atr => atr.name);
if (propsOfarray.indexOf(property.name) !== -1) {
numTimes++;
if (numTimes === list.length) {
items.push(property);
}
}
}
}
items.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
return items;
};