0

Here what is going on. I've got 4 objects. objA, objB, objC, objD.

objD, has multiple attributes and between them, objA, objB and objC.

class objD{
   constructor(objA, objB, objC, otherAttributes...){
        this.objA = objA;
        this.objB = objB;
        this.objC = objC;
        this.otherAttributes = otherAttributes;
        ...
   }
}

I have a Map() for each objAs, Bs, Cs as well as the objDs.

But, I've got to filter only the objAs, Bs and Cs that are used in objDs. After all, not all objAs, Bs and Cs are used, so, there is no point showing them in my options.

So, What I'm trying to achieve: Have 3 Sets from the used objAs, objBs, and objCs in a single Iteration. Of course that I could iterate 3 times the map of ObjDs. But I'd like to do that in a single iteration.

Here is what I've done so far:

To do a set of objAs for example:

let mapOfObjDs;  //this is the map containing all the objDs.
let setOfObjAs = new Set(Array.from(mapOfObjDs.values(), (x) => x.objA))

I manage to get an array 3 x n(n is the number of objDs):

 let map = Array.from(mapOfObjDs.values()).map((x) => 
        [
            x.objA,
            x.objB,
            x.objC
        ]);

But I have no Idea of how to convert this to 3 sets without iterating 3 times. Is it possible to do that in a single iteration?

2
  • 1
    How about adding a static-method or class-method to objD class that creates those sets as you instantiate the objects? Commented Oct 10, 2022 at 18:34
  • Good Idea, omg, I really didn't think about that jajajajaa. That would be much better, thanks for the advice. Commented Oct 10, 2022 at 18:37

2 Answers 2

1

You could create an array of sets and iterate the mapOfObjDs to insert each value.

For example:

// Map of objects
let mapOfObjDs = new Map([
    ["1", new objD("a1","b1","c1")],
    ["2", new objD("a2","b2","c2")],
    ["3", new objD("a3","b3","c3")],
    ["4", new objD("a4","b4","c4")]
]);

// Create empty array of set
let arrOfObjs = Array.from({length: 3}, (e) => new Set());

// Insert values
for (const objd of mapOfObjDs.values()) {
    arrOfObjs[0].add(objd.objA);
    arrOfObjs[1].add(objd.objB);
    arrOfObjs[2].add(objd.objC);
}

console.log(arrOfObjs);

output:

[
  Set(4) { 'a1', 'a2', 'a3', 'a4' },
  Set(4) { 'b1', 'b2', 'b3', 'b4' },
  Set(4) { 'c1', 'c2', 'c3', 'c4' }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Well thought. That would be also a good solution.
you can also create the class CustomMap extend map and rewrite the set(k,v) to build the array of sets when insert each objD. Will be more easy if you know you will not delete the objD
0

Something like this?

class objD{
   alldata = {
     A: new Set(),
     B: new Set(),
     C: new Set()
   }

   constructor(objA, objB, objC, otherAttributes...){
        this.objA = objA;
        this.objB = objB;
        this.objC = objC;
        this.otherAttributes = otherAttributes;
        
        alldata.A.add(objA);
        alldata.B.add(objB);
        alldata.C.add(objC);
   }
}

Then later you can access the lists via objD.alldata.A etc

1 Comment

Well that wouldn't work, because, lets say I've got in total 1000 objDs, 10 objAs, 20 objBs, 30 objCs. But, only 5 objAs, 10 objBs and 20objCs are used in objDs, In this method I'd have 3 arrays with 1000 objAs, 1000 objBs and 1000 objCs, and the Idea is to filter only the used objA, Bs and Cs. Thinking better, converting A, B and C arrays to Sets and leting alldata being static would be a way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.