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

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;

Related

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

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'));

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);

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
}

Get duplicates from array of objects by value [duplicate]

This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences
Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...
Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;

how can i define a object with Variables? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I want to add to Object with variables, like this
a = 'name'
object = {'age': 12, 'weight': 120}
I want this to
{'name': 'bob'}
I do this
object = {a: 'bob'}
but it give me
{'a': 'bob'}
how can I fixed it? I must use variables
Just assign it with the bracket notation after deleting the former content.
var a = 'name',
object = { 'age': 12, 'weight': 120 };
object = {}; // delete all properties
object[a]= 'Bob'; // assign new property
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');
You can't do this in one line. But you can do like this :
object = {};
object [a] = 'bob';
In ECMAScript 2015 there are computed property names:
var a = 'name';
var obj = {[a]:'fred'};
However there may not be sufficient support yet. See the MDN browser compatability table.

Categories