1

Is it a feasible solution to keep make arrays with _ids from mongodb database in front-end part? Let us suppose that I need to maintain the status of people with their ids . Is it good to associate things like this:

let arr=[];
arr['5bbaea8847910db52c7c3682']='p';
arr['5b9f6a1fd85effbb8acbd1fe']='a';
console.log(arr['5b9f6a1fd85effbb8acbd1fe']);

or Is It better to keep things like this :

 let arr=[];
 arr.push({
     _id:5bbaea8847910db52c7c3682,
     status:'p'
});

I fear if such big ids may lead to memory problems or such things. I have been a C++ programmer previously so it does not appear to be a cool thing to do. Is it OK to do such things in JavaScript ?

simhumileco
  • 31,877
  • 16
  • 137
  • 115
iamredpanda
  • 113
  • 3
  • 11

3 Answers3

1

As said above, u can use object of objects:

let data = {};
data[5bbaea8847910db52c7c3682] = {
   status: 'p'
}

Or a map:

let data = new Map();
data.set(5bbaea8847910db52c7c3682, { status: 'p' });
YarinDekel
  • 260
  • 1
  • 3
  • 8
0

Better to make arr as an object instead of array. Below is an example of creating new object as well as adding/editing it's property.

let obj = {} // or obj = new Object()
obj['5bbaea8847910db52c7c3682'] = 'p'
obj['5b9f6a1fd85effbb8acbd1fe'] = 'a'
console.log(obj)

To delete certain property, simply use delete keyword:

let obj = {}
obj['5bbaea8847910db52c7c3682'] = 'p'
delete obj['5bbaea8847910db52c7c3682']
console.log(obj)

if the property name is reserved keyword or it's contains a whitespace or special characters, then the way to set/get the property's value it's the same like set/get on array. Other than that, dot notation can be used.

let obj = {}

obj.someProperty = 'old value'
obj['someProperty'] = 'new value'

console.log(obj.someProperty) // 'new value'
console.log(obj['someProperty']) // 'new value'
novalagung
  • 10,905
  • 4
  • 58
  • 82
0

In this situation, it is better to use an array of objects (as you suggested), or simply an ordinary object:

let ob = {};
ob['5bbaea8847910db52c7c3682'] = 'p';
ob['5b9f6a1fd85effbb8acbd1fe'] = 'a';

console.log(ob['5b9f6a1fd85effbb8acbd1fe']);
simhumileco
  • 31,877
  • 16
  • 137
  • 115