DEV Community

giranezafiacre
giranezafiacre

Posted on

Dealing with objects in javascript

in javascript, we deal with objects a lot for example in reactjs there is no way you can implement react application without knowing how to manipulate objects. Also, there can be a possibility where you can use objects in sorting data from the database. even some styling frameworks use objects
here I am going to show some ways of using and manipulating objects.
we are going to use the example below:
var Fiacre={
'firstname':'Fiacre',
'lastname':'Giraneza',
'sex':'Male',
'height': 1.75,
'weight':65,
'favoriteColor':'brown'
}

To create new field

you may need to add field in object
Fiacre.skills='IT';
//another way
Fiacre['skills']='IT';
console.log('object with added field in Fiacre object:',Fiacre)

to update field

Fiacre.weight=70;
console.log('updated object:',Fiacre)

to read all keys

console.log('object keys:',Object.keys(Fiacre))

to read all values

console.log('object values:',Object.values(Fiacre))

to read one field

console.log('firstname field:{',Object.keys(Fiacre)[0],':',Object.values(Fiacre)[0],'}')

to read one key

console.log('firstname key:{',Object.keys(Fiacre)[0],'}')

to read on value

*console.log('firstname value:{',Object.values(Fiacre)[0],'}')
console.log('forth way to read firstname:{',Fiacre[Object.keys(Fiacre)[0]],'}')

console.log('third way to read firstname value:{',Fiacre['firstname'],'}')
console.log('forth way to read firstname value:{',Fiacre.firstname,'}')*

to delete field

delete Fiacre.favoriteColor;
delete Fiacre['sex'];
console.log('object without favoriteColor and sex:',Fiacre)

delete all values in fields

Object.keys(Fiacre).forEach(function(key){
Fiacre[key]='';
})
console.log('empty values in object:',Fiacre)

to delete all field

Object.keys(Fiacre).forEach(function(key){
delete Fiacre[key]
})
//or
Fiacre={};
console.log('empty object:',Fiacre)

to sort objects in an array based on certain property

you may need to sort objects in array which has same value for certain property.for example:

let arrayOfObjects=[{
'id':21058,
'full name':'Fiacre Giraneza',
'gender':'male'
},
{
'id':20938,
'full name':'Pacifique Tuyizere',
'gender':'male'
},
{
'id':20430,
'full name':'Clemence Nyiramariza',
'gender':'female'
},
{
'id':20311,
'full name':'Celine Mugeni',
'gender':'female'
}
];

const groups = arrayOfObjects.reduce((s, item) => {
const group = s[item.gender] || [];
group.push(item);
s[item.gender] = group;
return s;
}, {});
//if you display groups it will come as an object which has arrays inside
console.log(groups)

thank you Happy coding!!

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay