简体   繁体   中英

Find the longest array within an object (within another object) in javascript

I have an object of objects. Within those objects are arrays, that have more objects. Below is the structure.

I want to find out what the longest array of objects is within the parent object. So in my example below:

For ObjectOne - the longest array is B.

For ObjectTwo, the longest array is C.

mainobj = {
    ObjectOne: { 
        A: [ 
             { x: 'd', y: 'e'},
             { x: 'd', y: 2} 
           ],
        B: [ 
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'} 
           ],
        C: [ 
             { x: 'd', y: 'e'} 
           ]
    },
    ObjectTwo:{ 
        A: [
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'}
           ],
        B: [
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'}
           ],
        C: [
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'},
             { x: 'd', y: 'e'}
           ]
    }
}

I can do this:

var size = Object.values(mainobj).length

but I don't know how to get deeper than that. For instance, this doesn't work:

var size = Object.values(mainobj)[0].length

This question is pretty similar , but I can't figure out how to apply it. I appreciate the help!

You could return an object for each outer key and get an array with the largest inner array's keys.

 var object = { objectOne: { A: [{ x: 'd', y: 'e' }, { x: 'd', y: 2 }], B: [{ x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }], C: [{ x: 'd', y: 'e' }] }, objectTwo: { A: [{ x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }], B: [{ x: 'd', y: 'e' }, { x: 'd', y: 'e' }], C: [{ x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }, { x: 'd', y: 'e' }] } }, result = Object.keys(object).map(function (k) { var o = object[k]; return { key: k, items: Object.keys(o).reduce(function (r, l, i) { if (!i || o[r[0]].length < o[l].length) { return [l]; } if (o[r[0]].length === o[l].length) { r.push(l); } return r; }, []) }; }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can convert your objet to keep only arrays which have the max length:

 var mainObj = { 1:{ A: [ { x: 'd', y: 'e'}, { x: 'd', y: 2} ], B: [ { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'} ], C: [ { x: 'd', y: 'e'} ] }, 2:{ A: [ { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'} ], B: [ { x: 'd', y: 'e'}, { x: 'd', y: 'e'} ], C: [ { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'}, { x: 'd', y: 'e'} ] } }; var res = {}; Object.keys(mainObj).forEach(key => { var maxLength = 0, nameOfmaxLenghtArray, maxLenghtArray; Object.keys(mainObj[key]).forEach(arrayName => { if (mainObj[key][arrayName].length > maxLength) { maxLengthArrayName = arrayName; maxLenghtArray = mainObj[key][arrayName]; maxLength = mainObj[key][arrayName].length; } }); res[key] = { [maxLengthArrayName ]: maxLenghtArray}; }); console.log('res: ', res); // You can then access the max length array for each sub-object of mainobj: var subObject = '1'; var maxLengthArrayName = Object.keys(res[subObject])[0]; var maxLengthArray = Object.values(res[subObject])[0]; console.log(`max length array name for sub-object ${subObject}: `, maxLengthArrayName); console.log(`max length array for sub-object ${subObject}: `, maxLengthArray ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM