Accessing length of nested property [duplicate] - javascript

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I have this data and would like to access the length (i.e. number) of procedures nested within 'procedures'. How might I do this?
let specialties = [
{
name: 'Emergency)',
number: '1',
procedures: {
procedure1: 'PoPo',
procedure2: 'Lala',
procedure3: 'Dipsy',
}]

You can count the keys of the object:
let specialties = [
{
name: 'Emergency)',
number: '1',
procedures: {
procedure1: 'PoPo',
procedure2: 'Lala',
procedure3: 'Dipsy',
}
}
]
console.log(Object.keys(specialties[0].procedures).length)

Related

Redefine object properties keys starting with prefix [duplicate]

This question already has answers here:
What's the quickest way to rename all property names in a Javascript object?
(3 answers)
How to remove part of a string?
(7 answers)
JavaScript: Object Rename Key
(35 answers)
Closed 2 months ago.
If I have the following object, how can I redefine the keys to remove the prefix "Class"?
{
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
so it becomes
{
Name: "John",
Email: "john#doe.com",
Phone: "1234567"
}
Is there any easy way?
const obj = {
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
const newObj = Object.entries(obj).reduce((acc, item) => ({ ...acc,
[item[0].replace(/Class/g, "")]: item[1]
}), {})
console.log(newObj)

Sort an array by names with sort in TypeScript [duplicate]

This question already has answers here:
Sorting an array in Javascript based on predefined order
(3 answers)
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sorting on a custom order
(4 answers)
Closed 5 months ago.
var arr = [{
name: 'low'
},
{
name: 'middle'
},
{
name: 'urgent'
},
{
name: 'high'
},
{
name: 'none'
}
]
How do i sort this array based on the name and the order should be 1.urgent, 2.high, 3.medium, 4.low, 5.none

Insert object into dynamic key nested array [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

Partially replace value of key-val pair in an object [duplicate]

This question already has answers here:
Es6 merge two objects using spread not working
(5 answers)
How to deep merge instead of shallow merge?
(47 answers)
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 11 months ago.
Given I have the following object:
let obj = {
cat: "paws",
dogs: {
lab: "Christopher",
otherLab: "Lady Buba"
}
}
let objTwo = {
dogs: {
lab: "some new name"
}
}
How can I merge the above two objects so that the final result is:
{
cat: "paws",
dogs: {
lab: "some new name",
otherLab: "Lady Buba"
}
}
I've attempted to use spread operators with let finalObj = {...obj, ...objTwo}, but this entirely replaces the value of dogs, so that the end result is:
{
cat: "paws",
dogs: {
lab: "some new name",
}
}
which isn't what I want.

Find top level key in multidimensional object by value of second level key in javascript [duplicate]

This question already has answers here:
Find by key deep in a nested array
(21 answers)
Closed 5 years ago.
I have and multidimensional object that looks like this:
obj = {
'someString': {
name: 'John',
page: 'some url',
number: 4
},
'someString2': {
name: 'Bill',
page: 'some url',
number: 7
}
}
How do I find the first level key (in this case "someString2") where "number" is equal to 7?
The number is always unique and is the only thing I know beforehand.
Here you go. Using Array.find function to look for the appropriate key.
const numberToLookFor = 7;
const data = {
someString: {
name: 'John',
page: 'some url',
number: 4,
},
someString2: {
name: 'Bill',
page: 'some url',
number: 7,
},
};
const myKey = Object.keys(data).find(x => data[x].number === numberToLookFor);
console.log(myKey);

Categories