Reputation: 33
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();
Upvotes: 0
Views: 74
Reputation: 13417
The approach is as follows.
Object.values
Array.prototype.reduce
reduce
one also gets some of its default behaviors for free ...
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; }
Upvotes: 1
Reputation: 4208
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.
Upvotes: 0