This question already has answers here:
Pick random property from a Javascript object
(9 answers)
Closed 2 years ago.
const example1 = {
subex1: {
...something...
},
subex2: {
...something...
}
};
This is the code. For some reason, I can't use example1[Math.floor(Math.random() * example1.length)] code. How can I get a random item from sub constants (actually idk their name if they're not subconst).
You can do something like this.
const example1 = {
subex1: {
"hello": "hello"
},
subex2: {
"Hello1": "hello1"
}
};
console.log(Object.keys(example1)[Math.floor(Math.random()*Object.keys(example1).length)]);
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.
This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Accessing a JavaScript's object property without knowing that property name
(3 answers)
JavaScript: Get first and only property name of object
(7 answers)
How do I access properties of a javascript object if I don't know the names?
(8 answers)
Closed 3 years ago.
I have the following JavaScript object:
{
"_embedded": {
"dealerListItemDToes": [
{
...
},
{
...
}
]
}
}
Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.
How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?
Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:
const obj = {
"_embedded": {
"dealerListItemDToes": [
{
// ...
},
{
// ...
}
]
}
};
console.log(
Object.entries(obj._embedded)[0]
);
You can try like this
let data = {
"_embedded": {
"dealerListItemDToes": [{
"h": 1
}]
}
}
console.log(data._embedded[Object.keys(data._embedded)[0]])
This question already has answers here:
Object destructuring without var, let or const
(4 answers)
Is it possible to destructure onto an existing object? (Javascript ES6)
(16 answers)
Closed 3 years ago.
if the function calculateAvgGainLoss response includes avgGain, avgLoss we can get these values like this.
const { avgGain, avgLoss } = calculateAvgGainLoss(prices);
I want to set these values to the variable that defined outside of the function. How can I do that ? Example below.
describe("test", () => {
let avgGain: number, avgLoss: number;
it("Calculate average gain & loss", () => {
const prices = [...];
/*...This isn't working, has to be changed...*/ { avgGain, avgLoss } = calculateAvgGainLoss(prices);
expect(avgGain).toBe(0.24);
expect(avgLoss).toBe(0.1);
});
});
Beginning the line by a { confuses parsers (is it the beginning of a scope? an object? etc). Just bypass this by wrapping your line with parenthesis:
describe("test", () => {
let avgGain: number, avgLoss: number;
it("Calculate average gain & loss", () => {
const prices = [...];
({ avgGain, avgLoss } = calculateAvgGainLoss(prices)); // <--
expect(avgGain).toBe(0.24);
expect(avgLoss).toBe(0.1);
});
});
This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I have a JSON Structure like this:
{
"SOAP: Envelope": {
"SOAP: Header": "",
"xmlns: SOAP": "http: //schemas.xmlsoap.org/soap/envelope/",
"SOAP: Body": {
"ns0: Z_ZBC_USAGE_GET_DATA.Response": {
"IT_AREA_RANGE": "",
"ET_USAGE": {
"item": [...]
}
}
}
}
}
In my JS I try to access to the items
reports.data = data.SOAP:Envelope.SOAP:Body.ns0:Z_ZBC_USAGE_GET_DATA.Response.ET_USAGE.item;
This didn't worked because : signs are not allowed. What should be the correct expression to get the items?
Just access properties like array keys:
reports.data = data['SOAP:Envelope']['SOAP:Body']['ns0:Z_ZBC_USAGE_GET_DATA.Response'].ET_USAGE.item;