Categories
JavaScript Tutorials

Working with array of objects

Just like normal arrays, all array properties and methods are applicable for array of objects as well.

For example, let’s consider an array of objects and observe it’s length property:

var ar = [
           {"name":"Rahul"},
           {"name":"Harish"},
           {"name":"Anjali"},
           {"name":"Rajesh"}
         ];
console.log(ar.length); //4

Using the length property, we can loop through the array and print each object as we do in normal arrays:

var ar = [
           {"name":"Rahul"},
           {"name":"Harish"},
           {"name":"Anjali"},
           {"name":"Rajesh"}
         ];
for(var i=0;i<=ar.length-1;i++){
  console.log(ar[i]);
}
//{"name":"Rahul"}
//{"name":"Harish"}
//{"name":"Anjali"}
//{"name":"Rajesh"}

For example, if we want to print all the names, we will write the following code:

var ar = [
           {"name":"Rahul"},
           {"name":"Harish"},
           {"name":"Anjali"},
           {"name":"Rajesh"}
         ];
for(var i=0;i<=ar.length-1;i++){
  console.log(ar[i].name);
}
//'Rahul'
//'Harish'
//'Anjali'
//'Rajesh'

We can add more objects to the array of objects as follows:

var obj = {"name":"Priya"};
ar.push(obj);
console.log(ar);
/*
[
  {"name":"Rahul"},
  {"name":"Harish"},
  {"name":"Anjali"},
  {"name":"Rajesh"}
  {"name":"Priya"}
]
*/

Array of objects can also be sorted using the sort method. Let us take an array of objects:

var products = [
                 {
                   "brand": "Samsung",
                   "price": 70000
                 },
                 {
                   "brand": "Sony",
                   "price": 90000
                 },
                 {
                   "brand": "Apple",
                   "price": 80000
                 },
                 {
                   "brand": "Moto",
                   "price": 50000
                 }
               ];

If you want to sort the array of objects in ascending order of their prices, you need to write the following code:

products.sort(function(a, b){return a.price - b.price});

But if you want to sort the array of objects in alphabetical order of their brands, you need to modify the compare function as follows:

products.sort(function(a, b){
  let x = a.brand.toLowerCase();
  let y = b.brand.toLowerCase();
  if (x < y) {return -1;}
  if (x > y) {return 1;}
  return 0;
});

Here, we are converting the strings to lowercase before comparing to avoid conflict due to uppercase and lowercase letters.

Iterative methods

All iterative array methods are applicable with array of objects as well.

For example, we are applying forEach on the following array of objects to increase the salary of each employee by 10000:

var ar = [
	{
		"name": "Raju",
		"salary": 20000
	},
	{
		"name": "Harish",
		"salary": 30000
	},
	{
		"name": "Rajesh",
		"salary": 50000
	}
];
ar.forEach(function(emp){
	emp.salary = emp.salary + 10000;
});
console.log(ar);

The same can be achieved using map method as follows:

var br = ar.map(function(emp){
	return {"name": emp.name, "salary": emp.salary + 10000};
});
console.log(br);

To filter only those employees whose salary is more than 25000, you can use the filter method as follows:

var br = ar.filter(function(emp){
	return emp.salary > 25000;
});
console.log(br);

To check whether any employee is having salary less than 50000, you can use the some method as follows:

var check = ar.some(function(emp){
	return emp.salary < 50000;
});
console.log(check); //true

To check whether all employees are having salaries more than 30000, you can use the every method as follows:

var check = ar.every(function(emp){
	return emp.salary > 30000;
});
console.log(check); //false