Odd syntax for setting an object's property in JavaScript [duplicate] - javascript

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.

Related

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 }

How to add two words in a javascript function without using any library? [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 6 years ago.
How can 2 objects be added through a js function without using 'join' that the result output below is true? This is the code that I have now:
var twoObjs = function(obj1, obj2) {
return obj1 + obj2; // does not work
};
twoObjs("dog", "ball");
var output = mergeObjs({dog: "Max"}, {toy: "mouse"});
console.log(output.cat === "Max")
console.log(output.toy === "mouse")
You could use Object.assign to create a new object
var mergeObjs = function(obj1, obj2) {
return Object.assign({},obj1,obj2);
};
var output = mergeObjs({
dog: "Max"
}, {
toy: "mouse"
});
console.log(output.dog)
console.log(output.toy)

Cannot understand object with array as key [duplicate]

This question already has answers here:
Difference between ES6 object method assignment: a, 'a', and ['a']?
(2 answers)
Closed 6 years ago.
I've found some wild code on the web i don't understand:
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
})
What is [action.subreddit] doing? I thought that object keys had to be strings but this appears to be an array?
I'm hoping to understand mechanically how this code works.
thank you!
That's not an array as key, it's the es6 way to use a variable (/ a computed property) as the key.
Consider this:
var a = "foo";
function getKey() {
return "myKey";
}
var obj = {
[a] : "bar",
[getKey()] : "baz"
};
console.log(obj.foo); // bar
console.log(obj.myKey) // baz
So [action.subreddit] just sets the key's name to whatever value action.subreddit is holding.

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