javascript - How do I find Max length of object from Array Of Objects using Java script - Stack Overflow

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

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
}

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
}
Share Improve this question edited Jun 22, 2021 at 15:05 Alireza Ahmadi 9,9838 gold badges25 silver badges61 bronze badges asked Jun 22, 2021 at 14:36 Hari KrishnanHari Krishnan 931 gold badge2 silver badges10 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 3

I would use a reduce function on this to pare 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 pares 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);

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

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 pare for all object the length of the keys of the objects and return the object with more keys

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744181805a4562017.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信