Hari Krishnan
Hari Krishnan

Reputation: 93

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

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
}

Upvotes: 2

Views: 1846

Answers (5)

Alvie Mahmud
Alvie Mahmud

Reputation: 194

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);

Upvotes: 3

Alireza Ahmadi
Alireza Ahmadi

Reputation: 9903

Try this one

var array = [
  {
    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
  }
]
var lengthArray = array.map(t=> Object.keys(t).length)
var BigObjectIndex = lengthArray.indexOf(Math.max.apply(null, lengthArray))

console.log(array[BigObjectIndex])

Upvotes: 2

Timothy Alexis Vass
Timothy Alexis Vass

Reputation: 2705

let array = [{
    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
  }
]

let indexOfMaxKeys = 0;
let maxKeys = 0;

array.forEach(
  (item, index) => {
    if (Object.keys(item).length > maxKeys) {
      maxKeys = Object.keys(item).length;
      indexOfMaxKeys = index;
    }
  }
);

console.log(array[indexOfMaxKeys]);

Upvotes: 1

Ken La
Ken La

Reputation: 170

I'll do something like this :

const arrays = [
  {
    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 biggestObject = arrays.reduce((biggest, obj) => {
    if(Object.keys(biggest).length > Object.keys(obj).length) return biggest
    return obj
})

console.log(biggestObject)

I compare for all object the length of the keys of the objects and return the object with more keys

Upvotes: 2

Tushar Shahi
Tushar Shahi

Reputation: 20486

Loop through the array and find the number of keys each in the array has using Object.keys().

let max = null;
let maxLength = -Infinity;
x.forEach((a)=>{
  if(Object.keys(a).length > maxLength){
    max = a;
    maxLength = Object.keys(a).length;
  }
});

Upvotes: 2

Related Questions