Keeping array keys in JavaScript

array keys javascript
javascript key value array push
javascript associative array keys
javascript object keys values
javascript get value by key in array
find key in array javascript
javascript array
javascript array map

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 ?

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' });

Best way to store a key=>value array in JavaScript?, The keys of an Object are Strings and Symbols, where they can be any value for a Map. You can get the size of a Map easily while you have to manually keep track  The keys () method returns a new Array Iterator object that contains the keys for each index in the array. JavaScript Demo: Array.keys () var array1 = ['a', 'b', 'c']; var iterator = array1.keys (); for (let key of iterator) { console.log (key); // expected output: 0 1 2 } JavaScript Demo: Array.keys () The source for this interactive example

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'

Keeping array keys in JavaScript, As said above, u can use object of objects: let data = {}; data[​5bbaea8847910db52c7c3682] = { status: 'p' }. Or a map: let data = new Map();  The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. Skip to main content

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']);

JavaScript Array keys() Method, Create an Array Iterator object, only containing the keys of the array, and then loop through each key: var fruits = ["Banana", "Orange", "Apple", "Mango"]; Object.keys to get the keys, using parseInt to cast them as numbers. filter to get only ones you want. map to build an array from the original object of just the indices you're after, since Object.keys loses the object values. Debug: console.log(widthRange) console.log(relevantKeys) console.log(relevantValues)

PHP ksort() Function, Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Syntax. ksort(array, sorttype). Parameter Values. Parameter  I use Object.keys which is built into JavaScript Object, it will return an array of keys from given object MDN Reference var obj = {name: "Jeeva", age: "22", gender: "Male"} console.log(Object.keys(obj))

Converting Object to an Array, We have 3 variations to convert an Object to an Array in JavaScript. var numbers = { one: 1, two: 2, }; var keys = []; for (var number in numbers) { if (​numbers. Keep this in your knowledge box, but maybe wait a bit longer before you put it in  Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

array_merge - Manual, If an array key exists in both arrays, then the element from the first array will be used The array_merge function does not preserve numeric key values. This is a very common function, e.g. Javascript and Ruby have the `concat` function,  JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: However, what if you want to loop through the cars and find a specific one?

Comments
  • Your first code is abusing an array as an object. Definitely use an object instead, or an array of objects.
  • See this answer to learn what actually happens when you try to have a string as array index: stackoverflow.com/questions/9526860/…
  • if you just want to save a short key, maybe try using hash function?
  • @George I removed that tag .