Is there a way to add multiple properties into an object? [duplicate] - javascript

This question already has answers here:
How do I merge two dictionaries in Javascript? [duplicate]
(4 answers)
ECMAScript object spread/rest - assigning to multiple properties at once
(2 answers)
Closed 2 years ago.
For example given
const a = {
"a": 1,
"b": "hi",
}
const c = "54"
const d = "66"
I want a to be
a = {
"a": 1,
"b": "hi",
"c": 54,
"d": 66,
}
I want to do it in a single line so
a = {c, d}
But the above code will get rid of a, b. Any quick way to accomplish this?

Spread syntax
const a = {
a: 1,
b: 'hi',
};
const c = '54';
const d = '66';
console.log({ ...a, c, d });

One way is to use Object.assign:
a = Object.assign({}, a, { c, d });
This effectively creates a new object ({}), then it copies all properties from your initial object (a) then it copies the new properties ({c, d }).

Related

ReactJs Combine Key and Value [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
merge two arrays (keys and values) into an object [duplicate]
(3 answers)
Closed 2 years ago.
Platform: ReactJS
I am trying to combine the two arrays as a date:value object. Any thoughts?
a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
b = [ 1, 2, 3, 4 ]
I am looking for the following result:
["1/1/2020":1, "1/2/2020":2, "1/3/2020":3, "1/4/2020":4]
Thank you,
You can try this approach.
const a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
const b = [ 1, 2, 3, 4 ]
const c = {};
a.forEach((v, i) => {
c[v] = b[i]
});
console.log(c);

Is it possible to create an object from an array via destructuring? [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
Closed 3 years ago.
Is it possible to do this:
const foo = [1, 2];
const bar = {
a: foo[0],
b: foo[1],
c: 'something else not in array'
};
as a single statement, avoiding the need to declare foo?
As example, the array could be the result of
"1, 2".split(', ')
and you want to avoid the interim variable declaration, using "1" and "2" as the values for two of the properties (and potentially not the only properties) in a new object.
I can imagine something like this, though neither is valid for various reasons:
const bar { a, b, c: 'something else not in array' } = [1, 2];
or
const bar { a:[0], b:[1], c: 'something else not in array' } = [1, 2];
EDIT:
The closest I've found, without using an IIFE, is
Object.assign({c: 'something else not in array'}, [1, 2])
which has a negative in that rather than properties named 'a' and 'b', you get properties named '0' and '1':
{0: 1, 1: 2, c: "something else not in array"}
Yes, using an IIFE:
const bar = (([a, b]) => ({ a, b, c: "other props" }))([1, 2]);
If it concerns exactly two properties/values, then reduce can also work:
const bar = [1, 2].reduce((a, b) => ({ a, b, c: "other"}));

Destructure object into a smaller object [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
Elegant way to copy only a part of an object [duplicate]
(7 answers)
Closed 4 years ago.
I want to create a new object based on anther object but with fewer properties.
I know I can do it by manually assigment like this:
const obj = {
a: 1,
b: 2,
c: 3
};
const smallObj = {
a: obj.a
};
console.log(smallObj)
Is there a way to do it with destructuring?
I have tried doing this:
const obj = {
a: 1,
b: 2,
c: 3
};
const smallObj = {
a
} = {...obj}
console.log(smallObj, a)
But as you can see, I get the variable a to be equal to 1 but smallObj is a reference to obj.

convert an array of variable names into strings js [duplicate]

This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 4 years ago.
var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
array = [a,b,c,d,e] // outputs ["aa", "bb", "cc", "dd", "ee"];
However is there a possibility in javascript to convert the variables (a, b,c,d,e) into strings?
Like: "a", "b", "c", "d", "e"??
P.S: the array values could be dynamic as well or more than the length mentioned above.
Thanks for the help!!
You could do this with ES6 shorthand property names and return array of strings.
let a = "aa", b= "bb",c ="cc", d="dd", e="ee";
let strings = Object.keys({a, b, c, d, e});
console.log(...strings)
Something like this
var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
var array = [a,b,c,d,e];
({a,b,c,d,e} = array)
var keys = Object.keys({a,b,c,d,e});
console.log(keys)
console.log(array)

Javascript copy array of properties to another object (one-liner) [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(13 answers)
Closed 5 years ago.
This is obviously straight-forward using traditional loops, but I'm curious if anyone can think of a nice, compact way of doing it with destructuring and the spread operator.
For example, let's say I have
const foo = { a: 1, b: 2, c: 3, d: 4 };
const list = ['a', 'd'];
I'd like to create bar = { a: 1, d: 4 }.
You could do something like
const bar = {};
list.forEach((p) => { bar.p = list.p; });
But I'm wondering if anyone has a slick one-liner using ES2015+. For example, you can do const { b, c, ...bar } = foo, but that's if you have the inverse known prior to runtime.
While the solution ideally would support a dynamic list of properties in an array, one with a statically known list is better than nothing. (For example, const bar = ({{ a, d }} = foo)
The best I think you'll be able to do is with Array#reduce:
const foo = { a: 1, b: 2, c: 3, d: 4 };
const list = ['a', 'd'];
const bar = list.reduce((o, k) => (o[k] = foo[k], o), {})
console.log(bar)

Categories