2

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
}
0

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

Why set maxLength to -Infinity since Object.keys().length can only be positive ? Set it to 0 is more logic no ?
Yup. Basically any value that will be always less than equal to the lower bound value of Object.keys().length so 0 works. More of a habit with -Infinity.
2

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

Comments

2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.