Function that returns an array of strings that are in all arrays

4

Talk to people, all good?

My question is simple, but the solution may not be as simple as that.

The truth is that I am doing a job in JS and soon I will need to solve a problem that will make me lose a few hours breaking my head.

So I decided to ask here in StackOverflow to advance some of the solution.

Let's face it.

Suppose I have declared 5 arrays, then:

var objetos = ["abridor de garrafa", "abridor de latas", "adaga", "ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara", "zarabatana", "xilofone"];
var redondos = ["ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara"];
var cozinha = ["abridor de garrafa", "abridor de latas", "açucareiro", "xícara"];
var arma = ["adaga", "zarabatana"];
var musical = ["xilofone", "zabumba"]

Now, I need a function that when I pass 2 (or more) arrays, it returns me a new array, containing only the strings that are present in ALL the arrays I've passed.

Exemplifying:

If I give the function the arrays: "objects", "round" and "musical", the new array I receive should only contain "zabumba".

    
asked by anonymous 27.06.2017 / 14:32

2 answers

3

Good people, follow the solution to the problem:

var objetos = ["abridor de garrafa", "abridor de latas", "adaga", "ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara", "zarabatana", "xilofone"];
var redondos = ["ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara"];
var cozinha = ["abridor de garrafa", "abridor de latas", "açucareiro", "xícara"];
var arma = ["adaga", "zarabatana"];
var musical = ["xilofone", "zabumba"]

var arrays = [objetos, redondos, musical];

var result = arrays.shift().filter(function(v) {
    return arrays.every(function(a) {
        return a.indexOf(v) !== -1;
    });
});

document.write('<pre>' + JSON.stringify(result,null,4) + '</pre>');

If someone has a better solution and wants / can share, thank you!

    
27.06.2017 / 16:36
2

A more "manual" version because it will have to iterate all the items of the first array .

Example

var objetos = ["abridor de garrafa", "abridor de latas", "adaga", "ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara", "zarabatana", "xilofone"];
var redondos = ["ábaco", "abajur", "abotoadura", "acetona", "açucareiro", "zabumba", "xícara"];
var cozinha = ["abridor de garrafa", "abridor de latas", "açucareiro", "xícara"];
var arma = ["adaga", "zarabatana"];
var musical = ["xilofone", "zabumba"];

function comuns(...arrays){

    let aux = 1,
        possui = false,
        npossui = false,
        novo = [];
        
    arrays[0].forEach(function(item){
       
       aux = 1;
       npossui = false;
       
       while (arrays[aux]){
          if (arrays[aux].indexOf(item) > -1) possui = true;
          else npossui = true;
          aux++;
       }
       
       if (possui && !npossui) novo.push(item);
    });
    
    return novo;
}

console.log(comuns(objetos,redondos,musical));
    
27.06.2017 / 19:48