1

In following code i have define empty array houseTotal, now I would like to push value inside array which is unique and not exist previously. I have use some, unique, sort filter but its pushing all the value it gets. Here is my code:

            let houseTotal = [];
            await rel.map((each, index) => {
                if (
                    !isEmpty(each.house_detail) ||
                    !houseTotal.some(el => el === each.house_detail._id)
                ) {
                    
                    houseTotal.push(each.house_detail._id);
                }
            });
            return houseTotal.length;

What I have done mistake here ? Thank you.

11
  • Can you provide a reproducible example? What is rel and why are you using await on it? It doesn't appear to by async at all Commented Feb 10, 2021 at 6:51
  • @jnpdx Some query that returns house detail `let rel = await FamilyDetail.aggregate([...]); ' Commented Feb 10, 2021 at 6:56
  • Then why are you calling await on it again in your code above? Commented Feb 10, 2021 at 6:59
  • 1
    stackoverflow.com/help/minimal-reproducible-example Commented Feb 10, 2021 at 7:02
  • 1
    Still doesn't make sense for me why to await when defining let rel = await.. and then await again on await rel.map Commented Feb 10, 2021 at 7:15

2 Answers 2

1

If houseTotal is to have UNIQUE values and no duplicates.. I'm going to assume "duplicates" can be == each other and I'll use the Array.includes function

        let houseTotal = [];
        await rel.map((each, index) => {
            if (
                !isEmpty(each.house_detail) ||
                !houseTotal.some(el => el === each.house_detail._id)
            ) {
                let detail=each.house_detail._id
                if(!houseTotal.includes(detail)){houseTotal.push(detail);}
            }
        });
        return houseTotal.length;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks actually mistake was as you specified. It works fine with == and I fixed using toString() , both it works
0

And I got the solution, it was small mistake I have made on above code, I forgot to change object id returning from mongo to string so just added toString().

let houseTotal = [];
        await rel.map((each, index) => {
            if (
                !isEmpty(each.house_detail) ||
                !houseTotal.some(el => el === each.house_detail._id.toString())
            ) {
                
                houseTotal.push(each.house_detail._id.toString());
            }
        });
        return houseTotal.length;

2 Comments

um, uk toString isn't perfect.. but still.. why did you ask for help for something so specific to you when you knew the solution :{
Nah it took me 1 day to identify that problem, silly mistake and i throw that toString from my side too.

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.