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?
Related
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?
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
I have this object :
const list = {
id: 5,
name: "customer name",
projects: [
{
id:2,
title: "My project One",
studies: []
},
{
id:4,
title: "My project Two"
}
]
}
And I would like to be able to access a particular project in this object. There are only two of them here, but we imagine there could be more. How can I access a particular project by its ID? How can i do this ?
Thanks
You mention in the comments that the data is returned from an API. You can remap that data when it is received so that rather than a list of projects, it is an object keyed by the id field, which I assume is unique.
const allProjects = list.projects.reduce(
(collected, current) => {
return { ...collected, ...current };
}, {})
Failing that, you could use Array's filter to find the project you require:
const idToFind = 4;
const project = list.projects.filter( project => project.id === idToFind );
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
}
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 4 years ago.
I have an object which basically consists of some of the names of cars. I just want to delete the key of that object based on user input.
For instance:
let cars = {
car1: 'BMW',
car2: 'Lambo',
car3: 'Mercedes'
};
const deleteCar = (car) => {
delete cars.car;
}
deleteCar('car1');
console.log(cars);
As you can see, it doesn't actually delete the key from the object. How can I do this?
Use bracket ([]) notation which allows us to dynamically access property names:
let cars = {
car1: 'BMW',
car2: 'Lambo',
car3: 'Mercedes'
};
const deleteCar = (car) => {
delete cars[car];
}
deleteCar('car1');
console.log(cars);
foo.bar in JavaScript is equivalent to foo["bar"]. Thus, if car is a string, delete cars[car] does the correct thing (while delete cars.car tries to delete the literal key "car", which you don't have).
This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 8 years ago.
I have the following:
var allItems = {Items: [
{Value: 1, Name: "String1"},
{Value: 2, Name: "String2"},
{Value: 3, Name: "String3"}
]
};
localStorage.setItem('allItems', allItems);
I'm struggling to access any properties of allItems after retrieving it from localStorage. How would I say access the Name of the second object in the array Items?
You have to use JSON stringify to store the object and then use parse to convert it back into a JSON object.
localStorage.setItem('allItems', JSON.stringify(allItems));
var storageItems = JSON.parse(localStorage.getItem('allItems'));
console.log(storageItems.Items[1].Name);