This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
given this example:
var obj = {
'a': 1,
'b': 2
};
will be a:1, b:2 as excected. But what if I have:
var b = 'myProperty';
var obj = {
'a': 1,
b: 2
};
I wanted to have a: 1, myProperty: 2 but I still got a: 1, b: 2! How to fix it?
Variables in object property names are only allowed using the bracket notation:
var b = 'myProperty';
var obj = {
'a': 1
};
obj[b] = 2;
console.log(obj.myProperty); // logs 2
Hope this helps!
var b = 'myProperty';
var obj = {
'a': 1
};
obj[b]=2;
console.log(obj)
Related
This question already has answers here:
How can we custom sort JavaScript object keys?
(3 answers)
Closed 24 days ago.
I have the following Javascript object:
let obj = {
a: 1,
b: 2,
d: 4
}
obj["c"] = 3
When I log this, the keys are in the order a, b, d, c. Is there a way to make it so that c is inserted between b and d?
One method is to create a new object with Object.fromEntries after sorting the entries of the original object.
let obj = {
a: 1,
b: 2,
d: 4
}
obj["c"] = 3;
let newObj = Object.fromEntries(Object.entries(obj).sort());
console.log(newObj);
Alternatively, just sort the keys before looping over them.
let obj = {
a: 1,
b: 2,
d: 4
}
obj["c"] = 3;
for (const key of Object.keys(obj).sort()) {
console.log(key, '=', obj[key]);
}
i am having 2 objects and the code is
Object 1
var existingValue=self.get('objHubDto').ftd.gridDataDTO.gridDTOMap[name].gridDataMap;
Object 2
var newValue = self.get('childDataList')
I need to merge/add/append this two in newvalue value eventhough it having duplicate key values
Following is a very basic example to merge by keeping both values, however, you can create new object rather mutating existing one.
let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = { a: 9, d: 8, e: 7 };
Object.entries(obj2).forEach(([key, val]) => {
obj1[key] = obj1[key] ? [obj1[key], val] : val;
});
console.log(obj1);
I would love to "add/merge" (not sure how to call that) some objects in this manner:
obj1 = {
a: 1,
b: 1,
c: 1
}
obj2 = {
a: 1,
b: 1
}
obj1 + obj2 => { a: 2, b: 2, c: 1 }
Is there any way of achieving this? Tried, Object.assign(obj1, obj2) but It will not add properties like I need it to be done (it returns in { a: 1, b: 1, c: 1})
There isn't a built-in way to do it but you can enumerate the properties of one object and add their values to another.
const a = { a: 1, b: 1, c: 1 };
const b = { a: 1, b: 1 };
for(const prop in b) {
a[prop] = (prop in a ? a[prop] : 0) + b[prop];
}
console.log(a);
The prop in a check is so that we don't end up with NaN by adding undefined to the value in b.
You could use reduce to combine n number of objects like so:
const a = { a: 1, b: 1, c: 1 };
const b = { a: 1, b: 1 };
const c = { a: 1, c: 1 };
const result = [a, b, c].reduce((p, c) => {
for(const prop in c) {
p[prop] = (prop in p ? p[prop] : 0) + c[prop];
}
return p;
}, {});
console.log(result);
You didn't mention how you wanted to deal with properties in the prototype chain. You should know that for...in will enumerate properties in the prototype chain and prop in x will also examine the prototype chain. If your only want to enumerate the objects own properties then you could use Object.entries() to get its own properties or do a hasOwnProperty(...) check within the for...in and in place of the prop in x check. If you don't do any prototypal inheritance with your models then you may not care.
A quick answer:
let sum = {};
let keys = new Set(Object.keys(obj1))
Object.keys(obj2).map(x => keys = keys.add(x))
keys.forEach(x => {
let op1 = obj1[x] || 0;
let op2 = obj2[x] || 0;
sum[x] = op1 + op2;
})
Create an empty object:
var obj3 = {};
Use the spread operator to grab all the keys from both objects, then add them to the new object like so:
for(var i in {...obj1, ...obj2}) {
obj3[i] = (obj1[i] || 0) + (obj2[i] || 0);
}
var obj1 = {
a: 1,
b: 1,
c: 1
}
var obj2 = {
a: 1,
b: 2,
d: 3
}
var obj3 = {};
for(var i in {...obj1, ...obj2}) {
obj3[i] = (obj1[i] || 0) + (obj2[i] || 0);
}
console.log(obj3);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So i am trying to check if one object contains exactly another/ partial. For example, i want to check if { a: 1, b: 2, c: 2 } contains { a: 1, b: 2 }. But has to be in that order. If a is the first element and lines up, the b element will have to line up exactly afterwards. Plus is their anyway i could check there value? Order does matter.
//Here is my code currently:
function whatIsInAName(collection, source) {
var array = [];
var sourceNames = Object.getOwnPropertyNames(source);
for (var i = 0; i < collection.length; i++) {
var collectionNames = Object.getOwnPropertyNames(collection[i])
JSON.stringify(collectionNames).includes(JSON.stringify(sourceNames)) ? array.push(collection[i]) : null
}
return array
}
whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 });
Its a exact check but, only for the names. I wish it also compared the values.
It also isn't partially checking for the object in another. Like comaparing { a: 1, b: 2, c: 2 } to contain { a: 1, b: 2 }
Here was the challenge i was given --> https://www.freecodecamp.org/challenges/wherefore-art-thou
You coukd take the keys of the single object for comparing the values and use every for returning the result for filtering the array of objects.
Array#filter for getting parts of the array,
Object.keys for getting an array with own keys of the object,
Array#every`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) for checking all keys and values,
in operator for check if the key exist in the object,
Identity/strict equality operator === for checking type and value of the operands.
function compare(array, object) {
return array.filter(function (o) {
return Object.keys(object).every(function (k) {
return k in o && object[k] === o[k];
});
});
}
console.log(compare([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 }));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I was trying to guide you to figure out the problem on your own in the comments, but if you're just looking for a solution, here is how you can do this using basic JavaScript constructs (nothing fancy like Array#filter and Array#every).
It may be much longer than some other solutions, but it makes use of the basics of JavaScript:
function whatIsInAName(collection, source) {
var array = [];
var sourceNames = Object.getOwnPropertyNames(source);
// loop over each item in collection
for (var i = 0; i < collection.length; i++) {
var matchedProperties = 0;
// loop over each property in source
for (var j = 0; j < sourceNames.length; j++) {
// check whether the property is in collection[i] and
// whether the value matches
if (collection[i].hasOwnProperty(sourceNames[j]) &&
source[sourceNames[j]] === collection[i][sourceNames[j]]) {
matchedProperties += 1;
}
// add to array if all properties matched
if (matchedProperties === sourceNames.length) {
array.push(collection[i]);
}
}
}
return array
}
var result = whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 });
console.log(result);
I'd like to use lodash to selectively mutate properties in an object.
var foo = { 'a': 1, 'b': 2, 'c': 3 };
function addOne(num) {
return num + 1;
}
var propsToTransform = ['a', 'b'];
_(foo).pick(propsToTransfrom)
.map(addOne);
// I want foo = { 'a': 2, 'b':3, 'c':3 }
Is it possible to achieve this using the kind of composition I've outlined above or should I stick to something like
_.forEach(propsToTransform, (prop) => {
if (foo[prop]) {
foo[prop] = addOne(foo[prop]);
}
});
You are looking for _.mapValues and _.protoype.value as andlrc pointed out. You'll end up creating a new object with the new values and merge it with the original one :
var foo = { 'a': 1, 'b': 2, 'c': 3 };
var propsToTransfrom = ['a', 'b']
// Create a new object with the new, modified values and merge it onto the original one
var bar = _.merge(foo, _(foo).pick(propsToTransfrom).mapValues(addOne).value());
console.log(bar); // { 'a': 2, 'b': 3, 'c': 3 }
function addOne(num) {
return num + 1;
}