Skip to content
Advertisement

Tag: set

How do I use $addToSet with objects in mongodb?

I want to push only unique objects into an array with $addToSet. My userSchema has a field bankDetails which should take an array of objects: I am updating the schema using the following controller function: The data is saving correctly but Mongo auto generates an _id field every time so technically none of the objects are unique and the objects

Pushing array into an array of array in TS

I am coming from Python, and am not entirely familiar with the ‘proper’ JS/TS way of doing things. I am looping through the elements of a set, and I am pushing lists of some of the elements onto a 2D array. I am getting the following error TypeError: Cannot read properties of undefined (reading ‘push’). What am I doing wrong,

Sorted (ordered) collection for JavaScript

I’m looking for sorted container for JavaScript. I’m using C++ std::set, https://en.cppreference.com/w/cpp/container/set and try porting my code to JavaScript. JavaScript Map is not ordered container. I need some ordered container. I don’t need completely compatible container of std::set on C++. My requirements are Custom comparator support Automatically sorted Find the specific value. If value is not found, get the next

How to build a Set of unique Arrays?

I want to add many arrays to a Javascript set, and ensure that only unique arrays are added to the set. However, when I try adding the same array multiple times, it is always added instead of rejected. The .has() method always returns false as well. How do I fix this? Answer I’d use a Map instead, indexed by the

Array item gets the value “undefined”

I am trying to create an array of unique numbers from 0 to 10: But the output of the array will always have an undefined item at a random index which I don’t know why. Can somebody help me out? Answer If the new Set removes a duplicate, then i will be larger than the length of numbers, due to

Set of objects in javascript

I’d like to have a set of objects in Javascript. That is, a data structure that contains only unique objects. Normally using properties is recommended, e.g. myset[“key”] = true. However, I need the keys to be objects. I’ve read that Javascript casts property names to strings, so I guess I can’t use myset[myobject] = true. I could use an array,

Advertisement