Creating object with varying keys

javascript get object value by variable key
javascript use variable to access object property
javascript dynamic object key es6
javascript use variable value as object key
javascript object
create dynamic array with key and value in javascript
use variable as key in json
js object

I want to create object similar to this using loop, where I have separate array of country codes and another array of latitude and longitudes is there any way to achieve this in typescript,

var latlong={
            'us': {'latitude':39.528019315435685, 'longitude':-99.1444409552122},
            'fr': {'latitude':46.62065825554313, 'longitude':2.4521888685061306}
    }

Assuming that your arrays have corresponding index, then you can use this sample code,

let country = ['us','fr'];
let lat = ['39.528019315435685','46.62065825554313'];
let long = ['-99.1444409552122','2.4521888685061306'];

let latlong = {};

country.forEach((code,index)=>{
   latlong[code] = {};
   latlong[code]['latitude'] = lat[index];
   latlong[code]['longitude'] = long[index];
})

How to use a variable for a key in a JavaScript object literal?, { thetop : 10 } is a valid object literal. The code will create an object with a property named thetop that has a value of 10. Both the following are the same: Create an object variable. Declare an object variable. Assign an object variable to an object. Refer to the current instance of an object. See also. You can treat an object variable exactly the same as the object to which it refers. You can set or return the properties of the object or use any of its methods.


Assuming that both arrays are correctly mapped through their indices, you could try :

coordinatesArray.forEach((coordinate, index)=>{
    latlong[countryArray[index]] = coordinate;
});

where coordinatesArray contains an array of latitude and longitude objects and countryArray contains the country string.

ES6 Way of Creating Object with Dynamic Keys, Previously, we always had to use the bracket notation to use a dynamic key. With ES6, we can finally create dynamic variable key in the object declaration. To define an object type, create a function for the object type that specifies its name, properties, and methods. For example, suppose you want to create an object type for cars. You want this type of object to be called Car , and you want it to have properties for make, model, and year.


Here's small example of way to create desired object.

CodeSandbox-project

Basically you iterate through other array and with index reference to your other array. with obj[key] you can create object with variables as keys.

And just to open a bit discussion about achieving this in TypeScript. TypeScript basically only extends existing EcmaScript-syntax with types so this would work in vanillaJS or in TypeScript as expected. Benefit you get in TypeScript is if you would have typed your variables.

eg.

const country: string[] = ['fr','en']

So now your IDE can show you that you expect country-variable only to contain strings.

How to use a variable for a key in a JavaScript object literal , The only option you had was to create the object literal, assign the variable property name with value and pass the resulting object to the animate method. ES6  Creating an Object in Python. We saw that the class object could be used to access different attributes. It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call. >>> ob = MyClass() This will create a new instance object named ob. We can access attributes


JavaScript, In order to set an object's key by variable, here are few steps to follow. Steps: First make a variable. Then make a object. Assign that variable a value. Here are  When a JavaScript variable is declared with the keyword "new", the variable is created as an object: var x = new String(); // Declares x as a String object var y = new Number(); // Declares y as a Number object


Using JavaScript Variables as Object Keys, We could also create key, value pairs within the object, as our above the object on the server, using the above variable as the object key. Object.keys() is used for returning enumerable properties of an array like object. Object.keys() is used for returning enumerable properties of an array like object with random key ordering. Syntax: Object.keys(obj) Parameters Used: obj : It is the object whose enumerable properties are to be returned. Return Value: Object.keys() returns an


Objects, An empty object (“empty cabinet”) can be created using one of two syntaxes: let user The dot requires the key to be a valid variable identifier. Excel VBA Collection Object. We have seen the VBA Dictionary and VBA Array both have a similar connection with VBA Collection. In VBA Collection, we can create our own collection of group and provide the item and key to get the value stored under it.