How to pass multiple object attributes as parameter? [duplicate] - javascript

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Accessing nested arrays/properties in javascript
(3 answers)
Closed 2 years ago.
Let's use this object as example:
var obj = {a: {b: {c: 'result'}}}
I know that I can get the value of c, doing this:
console.log(obj.a.b.c) // 'result'
or this:
console.log(obj['a']['b']['c'])
but how I can get the value of c passing obj and columns as arguments in a function?
function func(obj, attributes) {
return obj[attributes]
}
console.log(func(obj, a.b.c)) // how to make this work
console.log(func(obj, ['a']['b']['c'])) // or this

You can pass attributes as string like 'a.b.c'. Then split it and use reduce to get desired value.
Test it below.
var obj = {a: {b: {c: 'result'}}}
function func(obj, attributes) {
return attributes.split('.').reduce((x, a) => x[a], obj);
}
console.log(func(obj, 'a.b.c'));
console.log(func(obj, 'a.b'));

Related

Creating a nested Javascript Object from an array of Strings [duplicate]

This question already has answers here:
Convert list of Objects when the levels are given in an array
(2 answers)
Populate nested object from array?
(2 answers)
Closed 3 years ago.
What's the best way to create a N levels nested object (where N is the size of the array) for example:
const arr = ['a','b','c','d']
The output object should look like this:
{
a: {
b: {
c: {
d: true
}
}
}
}
You can use array.reduce, it helps you pass an accumulator where you can accumulate your nested obj.
const array = ['a','b','c','d'];
const object = {};
array.reduce((o, s) => {
return o[s] = {};
}, object);
console.log(object);

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.

Why object keys passed as an array element throw a syntax error in javascript? [duplicate]

This question already has answers here:
JavaScript set object key by variable
(8 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
Say: arr1 = ['a', 'b', 'c'] and arr2 = [0, 1, 2]
We want an object {'a' : 0}
This function throws a syntax error:
function makeObject(arr1, arr2) {
return {arr1[0] : arr2[0]}
}
but this is okay:
function makeObject(arr1, arr2) {
return {[arr1[0]] : arr2[0]}
}
Why and where can I find more documentation on this behavior?

Look up object keys by value [duplicate]

This question already has answers here:
Swap key with value in object
(25 answers)
Closed 5 years ago.
Object = {"o":"s","e":"w"}
If I have this object, is there a way to perform reverse lookups on it?
Something like:
Object.invert()["s"]
> "o"
You want to revert the key/value mapping.
var test = {a: "b", c: "d"}
var reverted = {}
for(var key in test) {
reverted[test[key]] = key
}

destructure object in javascript es6 when keys are integers [duplicate]

This question already has answers here:
Object destructuring with property names that are not valid variable names
(2 answers)
Closed 6 years ago.
When we have an object in JS like
var obj = {a: "apple", p: "pen"};
then we can destructure it as follows
var {a, p} = obj; /* a = 'apple', p = 'pen' */
i want to know in case when keys are integers, how can we destructure it ? since we cannot declare integers as variable name
var obj = {0: 'pineapple', 1: 'pen'};
Just like any other assigning to new variable names
var {0:a, 1:b} = obj;

Categories