how does usage of braces change the value in javascript? [duplicate] - javascript

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Javascript object literal: what exactly is {a, b, c}?
(3 answers)
Closed last year.
I have the below Javascript code:
const product = await productsRepo.getOne(req.params.id);
console.log(product);
console.log({product});
Output from first console.log.
{ title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
Output from second console.log:
{
product: { title: 'hey12up', price: 1234, image: '', id: 'b6ff5da7' }
}
Though I understand that in the second output, product is a key and the value is an object (which are further key-value pairs), I dont understand how this conversion is done?
Just by enclosing the object name "product" inside braces, what is happening? Is there any technical term for this?

Related

access object property name as string [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 9 months ago.
I have an array of string, that I am trying to convert to an array of objects, on order to dynamically create a mat table, using the guide on the Angular Material Website
Stackblitz example from Angular Material, but they're not really doing it dynamically, as their columns are manually typed in the component
columnsToDisplay: string[] = [
'log_id',
'computer_id',
'processor_test',
'display_test',
'storage_test',
'network_test',
'keyboard_test',
'mouse_test',
'userfields',
'hard_disk_id',
'start_time',
'hard_disk_serial',
'gigabytes',
'target_drive_detailed',
'drive_detailed',
'target_drive',
'pattern_name',
'wipe_status',
'errors',
'dirty_sectors',
'tool',
'kernel',
'job_uuid',
'uuid',
'end_time',
'end_time_short',
'num_passes',
'trim_passes',
'sectors_overwritten',
'sectors_not_overwritten',
'sectors_verified',
'custom_field_legacy',
'nis_method_type',
'dco_foundremoved',
'dco_locked',
'hpa_foundremoved',
'amax_foundremoved',
'vendor',
'chassis',
'computer_model',
'computer_serial',
'computer_summary',
'motherboard_summary',
'cpu_summary',
'nic_summary',
'display_summary',
'usb_ports',
'computer_memory',
'hard_disk_drive_media_type',
'hard_disk_product',
'hard_disk_vendor',
];
this.columnsToDisplay.forEach((field: string) => {
const selected: boolean = this.displayedColumns.some((column: string) => column === field);
this.columnSelection.push({
value: field,
viewValue: field.toUpperCase().replace('_', ' '),
selected: selected,
cell: (element: ErasureModel) => `${element.[field]}
})
});
Obviously it is the element.[field] that is not working
Is it at all possible to generate this, so that for the field log_id I could have it say element.log_id?

Push an object into an array inside an object [duplicate]

This question already has answers here:
How can I get the full object in Node.js's console.log(), rather than '[Object]'?
(19 answers)
Closed 1 year ago.
First of all, I'm new in node js. I'm trying to create a REST API which looks like this:
[{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [
{
relay_name:"r1",
status:0
},
{
relay_name:"r2",
status:1
}
]
}
}]
But no matter what I'm trying it keeps on showing me below output:
[
{
id: 1,
name: 'tushar hasan',
email: 'mtushar**#gmail.com',
devices: {
id: 2,
device_mac: '4A:34:ER:34:12',
relays: [Array]
}
}
]
I'm providing you source code that's in below:
var x = [];
var data = {id :1, name:'tushar hasan', email:'mtushar**#gmail.com'};
var relay1 = {relay_name:'r1', status:0};
var relay2 = {relay_name:'r2', status:1};
var devices = {id:2, device_mac: '4A:34:ER:34:12'};
devices.relays = [];
devices.relays.push(relay1);
devices.relays.push(relay2);
data.devices = devices;
x.push(data);
console.log(JSON.stringify(x));
console.log(x);
By the way, when I call JSON.stringify(x), it provides me elements inside the relay array property. But without JSON.stringify(x) it doesn't show the elements inside relay property.
Thanks in advance.
Your code is fine. By default, when converting an object to string (like console.log does), the nested arrays will get converted to the text [Array]). The fact that you see the correct output when you call JSON.stringify asserts that it's really there.

Checking if an array of objects contains value without looping? [duplicate]

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
Right now I have an array that looks like this
const array = [
{
value: 'received',
title: 'Hjá Birgja',
},
{
value: 'pending',
title: 'Yfirstandandi',
},
{
value: 'processing',
title: 'Í vinnslu',
},
]
and I would like this to return true
if(array.includes('processing'){
// do something
}
not exactly what you wanted since i'm not sure if you only wanted to search the value key but here's a solution
if (array.find(i => i.value === 'processing')) {
// do something
}

What does this variable declaration form mean? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 3 years ago.
It is that situation, when you get some code, which works, but you don't know how.
What does this declaration method do?
const { actions: { createRole, updateRole } = {} } = props;
The code uses destructing for an nested object. The following example might help for understanding this new JavaScript syntax (has been introduced with ES6):
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
degree: 'Masters'
}
};
const {education: {degree}} = user;
console.log(degree); //prints: Masters
I would recommend the following resource for further examples:
https://medium.com/#pyrolistical/destructuring-nested-objects-9dabdd01a3b8

JavaScript Object property always returns undefined [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
Tell me what I am missing here. I have the follow javascript object.
[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD',
handle: '123',
userId: 'ABC123'} ]
When I do the following
success: function (registration) {
console.log(registration);
console.log(registration.handle);
Console log writes out the object as defined above. However when I do registration.handle I get an error saying "undefined." If registration is the above object why does registration.handle not work?
what am I missing?
You have an array containing an object. The properties you are trying to access are members of the object, not the array.
You must first get a reference to the object before you access its properties.
registration[0].handle
Try this
var registration=[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ]
alert(registration[0].handle)
DEMO
You are accessing the member of an object.
Do it like this way
success: function(registration) {
$.each(registration, function(index, data) {
var handle = data.handle;
console.log('id is getting now ' + handle);
});
}
Yes you first need to access array element then you can find object
console.log(registration[0].handle);
it is because you are having array so to access it try
registration[0].handle
EXAMPLE
CASE 1
registration = [ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ];
console.log(registration[0].handle);
CASE 2
registration = { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'};
console.log(registration.handle);

Categories

Resources