Destructing assignment with dots in property [duplicate] - javascript

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 5 years ago.
I have an object like this:
const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }
I would like to do a destructuring assignment. Is that only possible for this type of fields?
const { uuid } = myObject;
Thanks!

Variable names can't include a dot, so you can't get do const docs.count = 1000, for example. Destructuring allows you to extract the values even if the property name can't be a the name of a variable, but you'll need to assign them a valid variable name:
const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }
const { 'docs.count': docsCount } = myObject;
console.log(docsCount);

Related

How to get value from object using string of object path in typescript without using eval? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 29 days ago.
In JS/typescript, lets say I have an object like this
var obj = {
a: {
b:{
c:1
}
}
}
And I have a string "b.c". How can I evaluate this using only those variables and get the value 1 from the object without using hacks like eval since typescript would complain.
Thanks
It I understood you question, the keys are known, if so you can use the '[]' to access it :
const a = b.c
is the same thing as
const a = b['c']
but in the second case you can of course use a variable.
Applying this to your object, it would look something like this :
const obj = {
a: {
b: {
c: 1
}
}
}
const outerKey = 'a'
const middleKey = 'b'
const innerKey = 'c'
const value = obj[outerKey][middleKey][innerKey]
console.log(value)
Hope it helped you !

How to compare if second object has some value in first object then return true? [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
Closed last year.
I want to compare two objects as below:
First object:
const permissions = {
"statistics":{"list":"1"},"audit":{"list":"1"}}
}
Second object:
const userPermission =
{
"audit":{"list":"1"}
}
So, if the second object has some value the same as the first object then return true.
In this sample I want it return true. Becuase, audit has same properties.
var obj1 = {
name:'hello',
age:12,
fav: 'fav',
foo: 'foo'
}
var obj2 = {
name: 'hey',
say: 'say',
prop: 'prop',
top: 'top'
}
var common = Object.keys(obj1).filter(obj1item => Object.keys(obj2).indexOf(obj1item) !== -1 );
console.log(common);
You can look into this > Is there a way to check and see if two objects have properties in common?

JSON Object key value pair function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 3 years ago.
Why i am unable to pass "abc" to someKey
function convertToKeyValuePair(someKey,someValue){
var map = {someKey : someValue};
return JSON.stringify(map);
}
print(convertToKeyValuePair("abc","xyz"));
O/P = {"someKey":"sdfdf"}
Expected O/P = {"abc":"xyz"}
As you’re passing key dynamically so you’ve to use bracket around it like:
{ [someKey]: someValue }
You want dynamic keys.
let key = 'yo';
let obj = {key: 0}; // creates {"key": 0}
let objDynamic = {[key]: 0}; // creates {"yo": 0};
console.log(obj);
console.log(objDynamic);
As answered by the comment of #junvar, passing keys to objects without quotes is syntactic sugar and both of the following examples will give the same result:
{ "someVar": someValue }
{ someVar: someValue }
To use the value of a variable as a key you have to use square brackets as in:
{ [someVar]: someValue }

Property Name gets lost? [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 4 years ago.
I am working on a pre-boot camp coding problem. I am passing in an object (ourDog) and a key (fiendly) into a function that creates a new property (key:value pair) and assigns a value of true to that key.
Looking at the output, the property is added, but the key does not have the assigned value of 'friendly', it has the parameter name of key. I expected the key:value pair to (friendly: true). Here is the code.
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
function addProperty(anObject, key) {
anObject.key = true;
return anObject;
};
var output = addProperty(ourDog, 'friendly');
console.log(output);
{name: "Camper", legs: 4, tails: 1, friends: Array(1), key: true}
Change it from dot notation to brackets.
function addProperty(anObject, key) {
anObject[key] = true;
return anObject;
};
Dot notation is used to access an object's properties when the property is a valid JavaScript identifier. Since 'friendly' is a string, you need to use brackets.

Creating an object with dynamic keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Here is a function building an object dynamically:
function onEntry(key, value) {
console.log(key) // productName
console.log(value) // Budweiser
const obj = { key: value }
console.log(obj) // { key: "Budweiser" }
}
Expected output is
{ productName: "Budweiser" }
But property name is not evaluated
{ key: "Budweiser" }
How to make property name of an object evaluated as an expression?
Create an object, and set its key manually.
var obj = {}
obj[key] = value
Or using ECMAScript 2015 syntax, you can also do it directly in the object declaration:
var obj = {
[key] = value
}

Categories

Resources