简体   繁体   中英

how to filter arrays inside an object and return the array with the most items inside?

I'm developing an app that requests data from a third-party source by API calls. one of my relevant data is located in arrays inside an object.

The trick here is that in some calls I'm making to get the data that object contains a single array and in other calls, it contains multiple arrays.

I need the data of the array with the most items inside.

in most cases that object contains 2 arrays inside - in that case, my code is working well and I can filter my relevant array most of the time it's the second array - array[1].

but when that object contains a single array inside - that is where I'm struggling to achieve the data.

(The arrays names are random numbers in each JSON I'm getting so I need a generic solution).

here is example

object{

 "154987" [150 items],
 "754896" [13 items],
 "265489" [11 items]

}

Here is what I have in my code so far which not working with only single array

   function getCurrentBsrByObjectKeyIndex(index) {
      product.bsrObjectKey = (Object.keys(asinData.products[0].salesRanks)[index]);
      product.bsrHistory = asinData.products[0].salesRanks[product.bsrObjectKey];
      product.currentBsr = product.bsrHistory[product.bsrHistory.length-1];
    }
    function correctBsrObjectKey() {
      getCurrentBsrByObjectKeyIndex(1);
      if (product.bsrHistory.length < 15){
        getCurrentBsrByObjectKeyIndex(0);
      }
    }
    correctBsrObjectKey();

The approach is as follows.

  1. Directly access a list (an array) of all of the objects first level array's by using Object.values
  2. Iterate the list/array via Array.prototype.reduce

 function getArrayOfMaximumLength(obj) { return Object.values(obj).reduce((maxArr, arr) => // this implementation breaks at // an entirely emtpy `values` array ((maxArr.length > arr.length) && maxArr) || arr // // this implementation does never break but always // // at least returns an empty array... []... // // which might unwantedly shadow the consumption of // // broken data structures // // ((maxArr.length > arr.length) && maxArr) || arr, [] ); } const sample_1 = { "754896": ['foo', 'bar', "baz"], "154987": ['foo', 'bar', "baz", "biz", "buz"], "265489": ['foo'], }; const sample_2 = { "265489": ['foo'], "754896": ['foo', 'bar', "baz"], }; const sample_3 = { "754896": ['foo', 'bar', "baz"], }; const invalid_sample = {}; console.log( 'getArrayOfMaximumLength(sample_1)...', getArrayOfMaximumLength(sample_1) ); console.log( 'getArrayOfMaximumLength(sample_2)...', getArrayOfMaximumLength(sample_2) ); console.log( 'getArrayOfMaximumLength(sample_3)...', getArrayOfMaximumLength(sample_3) ); console.log( 'getArrayOfMaximumLength(invalid_sample)...', getArrayOfMaximumLength(invalid_sample) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

var object = {
    "154987": [1, 2, 3],
    "754896": [1, 2],
    "265489": [5, 4, 3, 2, 1, 2, 4, 5]
}
var keys = Object.keys(object);
var highestArray = [];
for (var i = 0; i < keys.length; i++) {
    var obj = object[keys[i]];
    if (Array.isArray(obj) && obj.length > highestArray.length)
        highestArray = obj;
}
console.log(highestArray);

Get all property name from your object. Then iterate through it to find the array with highest number of items in it.

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