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
}
Related
This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
(21 answers)
Closed 10 months ago.
I have an object with these key-value pairs. This object comes from an API which I am calling :
APIName(obj).subscribe((res:any) => {
console.log(Object.values(res.data));
})
data = {
0 : 1,
1: 3,
2: 7,
3: 10
....so on
}
simply put it is an object of numbers and my desired output is (a simple array) :
data = [1,3,7,10]
I've tried Object.value and Object.key it still converts it into an object. Any help?
First of all if your data is not sorted by key. Then sort it. and iterate through object and push to array.
var data = {
1: 1,
0: 3,
2: 7,
3: 10
};
let sortedKeys = Object.keys(data).sort();
let arrayOfData = [];
// iterate method
sortedKeys.forEach((key) => {
arrayOfData.push(data[key]);
});
console.log(arrayOfData);
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'));
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);
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.
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;