-1

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" }];
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Amazon
  • 406
  • 5
  • 14
  • Does this answer your question? [Difference and intersection of two arrays containing objects](https://stackoverflow.com/questions/33356504/difference-and-intersection-of-two-arrays-containing-objects) – pilchard Apr 19 '22 at 08:23
  • that's true for 2 array, but i have many array. – Amazon Apr 19 '22 at 08:28
  • just `reduce()` them all and keep taking intersections of the accumulator. – pilchard Apr 19 '22 at 08:30

4 Answers4

1

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.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
1
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)
Ilian Futekov
  • 174
  • 1
  • 8
0

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);
Amazon
  • 406
  • 5
  • 14
0

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);
Hao Wu
  • 17,573
  • 6
  • 28
  • 60