Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find Max length of object from Array Of Objects using Java script

Tags:

javascript

I have an array of objects.

I need to find which object's length is the maximum one from the array of objects.

Sample array of objects below

[
  {
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADDRESS2: "16, MOGUL LANE,MATUNGA - WEST",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  },
  {
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  },
  {
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  }
]


Here, the first object length is 6. The rest of them are smaller than it.

I need output of which object length is bigger one. Like below:

{
   ACCOUNTTYPE: "Pool Account",
   ADDRESS1: "D-102, PALM HOUSE",
   ADDRESS2: "16, MOGUL LANE,MATUNGA - WEST",
   ADVISORID: 3,
   ADVISORNAME: "Advisor",
   ARNID: -1
}
like image 616
Hari Krishnan Avatar asked Feb 04 '26 11:02

Hari Krishnan


1 Answers

I would use a reduce function on this to compare them and return a single object once it goes through them to find the largest one

const jsonArray = [{
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADDRESS2: "16, MOGUL LANE,MATUNGA - WEST",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  },
  {
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  },
  {
    ACCOUNTTYPE: "Pool Account",
    ADDRESS1: "D-102, PALM HOUSE",
    ADVISORID: 3,
    ADVISORNAME: "Advisor",
    ARNID: -1
  }
];

const objectWithMostAttributes = jsonArray.reduce(
  (objectWithMostAttributes, nextObject) => {
    // use Object.keys(object).length to get number of attributes of the object
    // reduce goes through the objects, and will set the first item as the first element of the array
    // and the second item as the second element of the array

    // Depending on what is inside the function, you can decide how it chooses what to return
    // The below code uses Object.keys(object.length) to get the number of attributes of the objects and compares them
    // It uses a ternary operator, it checks if the objectWithMostAttributes is more than or equal to the nextObject
    // If it is, return objectWithMostAttributes (to be used as the next objectWithMostAttributes),
    // else return the nextObject as the (to be used as the next objectWithMostAttributes)

    // This will continue until it gets to the end of the array
    return (Object.keys(objectWithMostAttributes).length >= Object.keys(nextObject).length) ? objectWithMostAttributes : nextObject;
  }
);

console.log(objectWithMostAttributes);
like image 115
Alvie Mahmud Avatar answered Feb 06 '26 23:02

Alvie Mahmud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!