0

I have one array and one object:

const currentData = [{first: 'Breanna', last: 'Bazadoo', uid: '-Lz8YxHiwp8QZW3TqAFn'},
{first: 'Foo', last: 'Bazado', uid: '-Lz8YxHqQXWoaGOFRLrO'},
{first: 'Foo2', last: 'Bazado', uid: '-Lz8YxHsMItaaTVNyQRE'},
{first: 'Foo3', last: 'Bazado', uid: '-Lz8YxHy0S-QkDaE1PkX'}
];

const ministryMembers = { -MmFNu4jQ8A4_Aig0vdn: "-MmFNu4jQ8A4_Aig0vdn",
-Mxg8xKwZleW4J8yfzna: "-Lz8YxHqQXWoaGOFRLrO",
-Mxg9AxhUR6ZArBATw1M: "-Lz8YxHsMItaaTVNyQRE",
-Mxg9ECyBsj01zMqPE8s: "-MmFNgjyopU3E8z5g-zU",
-Mxg9GvmPFabK_185IOr: "-Lz8YxLVi_uZp_RkcRIH",
-Mxg9JbJ2sxgWLxk1tfx: "-MuEgimJOIbPw3GyKBJ3",
-Mxg9MZTKzlUD0lg07C0: "-Lz8YxHy0S-QkDaE1PkX",
-Mxg9Pnfmv7P3BQvINQV: "-Lz8YxJ-_wuk8bmEyvGT",
-Mxg9SB2vlOopJYtmG-Z: "-Lz8YxKj5WXY1oNwylQ4",
-Mxg9VrZFDNawvcWIgul: "-Lz8YxN87U1YM6_fKgq1" }

if currentData's uid is found in allMembers value in example currentData uid == allMembers value, I want to return the entire object from currentData and build the array again. There will be a lot more in currentData this is just an example.

I have done this:

const filteredData = Object.keys(ministryMembers).filter( m => currentData.find( cd => cd.uid == ministryMembers[m] ) );
console.log(filteredData);

It returns only the key. How can I get it to return the whole object? My other option was map the data and then return the whole object that way but I thought that there might be a more efficent way of doing it. Any thoughts?

3
  • in object allMembers, is uid key or value ? and what is ministryMembers ? Commented May 27, 2022 at 6:20
  • value and sorry I fixed it allMembers was supposed to be ministryMembers Commented May 27, 2022 at 6:46
  • what is expect data for you !? Commented May 27, 2022 at 6:56

4 Answers 4

1

Does it work?

var valueList = Object.values(ministryMembers);
var filteredData = currentData.filter(e => valueList.includes(e.uid));

JsFiddle: https://jsfiddle.net/03bpak9m/2/

Sign up to request clarification or add additional context in comments.

Comments

0

Try this -

currentData.filter(e => typeof allMembers[e.uid] !== 'undefined')

or

currentData.filter(e => allMembers.hasOwnProperty(e.uid))

2 Comments

Only returns one allMembers should be ministryMembers which is an ojject. e.uid should be the value of allMembers
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

If there is large data then you can process it in chunks.

var filteredArray = [];
function processData(data, allMembers) {
  // process data in chunks -> 100 at a time
  let chunks = data.splice(0, 100);
  chunks.forEach((element) => {
    if (Object.keys(allMembers).find((e) => allMembers[e] === element.uid)) {
      filteredArray.push(element);
    }
  });
  if (data.length > 0) {
    setTimeout(() => {
      processData(data,allMembers);
    }, 0);
  } else {
    // processing complete
    console.log(filteredArray);
  }
}

var currentData = [{first: 'Breanna', last: 'Bazadoo', uid: '-Lz8YxHiwp8QZW3TqAFn'},
{first: 'Foo', last: 'Bazado', uid: '-Lz8YxHqQXWoaGOFRLrO'},
{first: 'Foo2', last: 'Bazado', uid: '-Lz8YxHsMItaaTVNyQRE'},
{first: 'Foo3', last: 'Bazado', uid: '-Lz8YxHy0S-QkDaE1PkX'}
];

var ministryMembers = { "-MmFNu4jQ8A4_Aig0vdn": "-MmFNu4jQ8A4_Aig0vdn",
"-Lz8YxHiwp8QZW3TqAFn": "-Lz8YxHqQXWoaGOFRLrO",
"-Lz8YxHqQXWoaGOFRLrO": "-Lz8YxHsMItaaTVNyQRE",
"-Mxg9ECyBsj01zMqPE8s": "-MmFNgjyopU3E8z5g-zU",
"-Mxg9GvmPFabK_185IOr": "-Lz8YxLVi_uZp_RkcRIH",
"-Mxg9JbJ2sxgWLxk1tfx": "-MuEgimJOIbPw3GyKBJ3",
"-Mxg9MZTKzlUD0lg07C0": "-Lz8YxHy0S-QkDaE1PkX",
"-Mxg9Pnfmv7P3BQvINQV": "-Lz8YxJ-_wuk8bmEyvGT",
"-Mxg9SB2vlOopJYtmG-Z": "-Lz8YxKj5WXY1oNwylQ4",
"-Mxg9VrZFDNawvcWIgul": "-Lz8YxN87U1YM6_fKgq1" };

processData(currentData,ministryMembers);

Comments

0

Object.keys(ministryMembers) return an array of keys

Object.keys(ministryMembers).filter(...) filter in above array

What is expect result for you !??

If you want data from currentData try it :

const currentData = [
  {first: 'Breanna', last: 'Bazadoo', uid: '-Lz8YxHiwp8QZW3TqAFn'},
  {first: 'Foo', last: 'Bazado', uid: '-Lz8YxHqQXWoaGOFRLrO'},
  {first: 'Foo2', last: 'Bazado', uid: '-Lz8YxHsMItaaTVNyQRE'},
  {first: 'Foo3', last: 'Bazado', uid: '-Lz8YxHy0S-QkDaE1PkX'}
]

const ministryMembers = { 
  '-MmFNu4jQ8A4_Aig0vdn': "-MmFNu4jQ8A4_Aig0vdn",
  '-Mxg8xKwZleW4J8yfzna': "-Lz8YxHqQXWoaGOFRLrO",
  '-Mxg9AxhUR6ZArBATw1M': "-Lz8YxHsMItaaTVNyQRE",
  '-Mxg9ECyBsj01zMqPE8s': "-MmFNgjyopU3E8z5g-zU",
  '-Mxg9GvmPFabK_185IOr': "-Lz8YxLVi_uZp_RkcRIH",
  '-Mxg9JbJ2sxgWLxk1tfx': "-MuEgimJOIbPw3GyKBJ3",
  '-Mxg9MZTKzlUD0lg07C0': "-Lz8YxHy0S-QkDaE1PkX",
  '-Mxg9Pnfmv7P3BQvINQV': "-Lz8YxJ-_wuk8bmEyvGT",
  '-Mxg9SB2vlOopJYtmG-Z': "-Lz8YxKj5WXY1oNwylQ4",
  '-Mxg9VrZFDNawvcWIgul': "-Lz8YxN87U1YM6_fKgq1" 
}
// two ways for it
const filteredData = Object.entries(ministryMembers).reduce((res, [key, value]) => {
  let currData = currentData.find(ele => ele.uid === value)
  return typeof currData == 'undefined' ? res : [...res, currData]
}, [])
console.log(filteredData)

const filteredData1 = currentData.filter( m => Object.entries(ministryMembers).some(([key, value]) => value == m.uid ))
console.log(filteredData1)

const filteredData2 = currentData.filter(e => Object.values(ministryMembers).includes(e.uid));
console.log(filteredData2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.