Home > Enterprise >  Javascript intersection with many array into array
Javascript intersection with many array into array

Time:04-20

I have an object like this;

var authList = {
  "first": [{
    Id: 1,
    name: "test1"
  }, {
    Id: 2,
    name: "test2"
  }, {
    Id: 3,
    name: "test3"
  }],
  "second": [{
    Id: 3,
    name: "test3"
  }],
  "third": [{
    Id: 2,
    name: "test2"
  }, {
    Id: 3,
    name: "test3"
  }],
  ...may be n unit
};

how do i get users that are in each array?

example out;

response = [{ Id: 3, name: "test3" }];

CodePudding user response:

In set theory terms you want to find the intersection between the sets of users. While JavaScript can transform arrays into sets, it does not have an intersection function but you can mix arrays and sets and use filters instead:

var authList = {
  "first": [{
    Id: 1,
    name: "test1"
  }, {
    Id: 2,
    name: "test2"
  }, {
    Id: 3,
    name: "test3"
  }],
  "second": [{
    Id: 3,
    name: "test3"
  }],
  "third": [{
    Id: 2,
    name: "test2"
  }, {
    Id: 3,
    name: "test3"
  }], 
};

let values = Object.values(authList);
let result = values[0].map(x => x.Id);
for(let i=1; i<values.length;i  ) {result = result.filter(x => new Set(values[i].map(x => x.Id)).has(x))}
console.log(result);

This snippet creates an array of ids and stores them in the result variable, in this case the result = [3]. If you want the whole object instead you can use a map to map identifiers to objects again.

CodePudding user response:

var authList = {
    "first": [{
        Id: 1,
        name: "test1"
    }, {
        Id: 2,
        name: "test2"
    }, {
        Id: 3,
        name: "test3"
    }],
    "second": [{
        Id: 3,
        name: "test3"
    }],
    "third": [{
        Id: 2,
        name: "test2"
    }, {
        Id: 3,
        name: "test3"
    }],
};

let users = []
Object.keys(authList).map((res) => {
    authList[res].map(res => {
        users.push(res)
    })
})

console.log('users', users)

CodePudding user response:

Here's a solution from pilchard's idea. Utilizing Array#reduce function to traverse the entire value set and find out the common items:

var authList = {
    "first": [{
        Id: 1,
        name: "test1"
    }, {
        Id: 2,
        name: "test2"
    }, {
        Id: 3,
        name: "test3"
    }],
    "second": [{
        Id: 3,
        name: "test3"
    }],
    "third": [{
        Id: 2,
        name: "test2"
    }, {
        Id: 3,
        name: "test3"
    }]
};

const commonItems = Object.values(authList).reduce((acc, cur) =>
    acc.filter(obj1 => cur.find(obj2 =>
        Object.entries(obj2).every(([k, v]) => obj1[k] === v))
    )
);

console.log(commonItems);

CodePudding user response:

this code works

            var authList = {
                "first": [{ Id: 1, name: "test1" }, { Id: 2, name: "test2" }, { Id: 3, name: "test3" }],
                "second": [{ Id: 3, name: "test3" }],
                "third": [{ Id: 2, name: "test2" }, { Id: 3, name: "test3"],
            };

            var keys = Object.keys(authList);
            var resultArray = null;
            for (var j = 0; j < keys.length; j  ) {
                if (!resultArray) {
                    resultArray = authList[keys[j]];
                }
                var sArray = authList[keys[j   1]];
                if (sArray) {
                    resultArray = resultArray.filter(a => sArray.some(b => a.Id === b.Id));
                }
            }
            console.log(resultArray);
  • Related