I currently have an array that looks like this:
var array = [
{
"title": "Ender's Game",
"genre": "Science Fiction",
"year": 2008
},
{
"title": "Harry Potter and the Goblet of Fire",
"genre": "fantasy",
"year": 2002
},
{
"title": "Pride and Predjudice",
"genre": "romance",
"year": 1980
}
]
I want to iterate through this array and find the length of the longest genre. I want something like this:
longestGenre(array);
15
Ive tried a few things including the following:
var getLongest = (array, key) =>
{
let r = Object.values(array).reduce((res, curr) =>
{
if (!Array.isArray(curr))
return res;
let newMax = curr.reduce(
(r, c) => c[[key]] && (c[[key]].length > r.length) ? c[[key]] : r,
""
);
res = newMax.length > res.length ? newMax : res;
return res;
}, "");
longest = r;
return longest;
}
And another approach I tried was this:
for (key in obj){
if (!obj.hasOwnProperty(key)) continue;
if (obj.key.length > longest)
longest = obj.key.length;
}
I currently have an array that looks like this:
var array = [
{
"title": "Ender's Game",
"genre": "Science Fiction",
"year": 2008
},
{
"title": "Harry Potter and the Goblet of Fire",
"genre": "fantasy",
"year": 2002
},
{
"title": "Pride and Predjudice",
"genre": "romance",
"year": 1980
}
]
I want to iterate through this array and find the length of the longest genre. I want something like this:
longestGenre(array);
15
Ive tried a few things including the following:
var getLongest = (array, key) =>
{
let r = Object.values(array).reduce((res, curr) =>
{
if (!Array.isArray(curr))
return res;
let newMax = curr.reduce(
(r, c) => c[[key]] && (c[[key]].length > r.length) ? c[[key]] : r,
""
);
res = newMax.length > res.length ? newMax : res;
return res;
}, "");
longest = r;
return longest;
}
And another approach I tried was this:
for (key in obj){
if (!obj.hasOwnProperty(key)) continue;
if (obj.key.length > longest)
longest = obj.key.length;
}
Share
asked Jan 24, 2019 at 16:56
MandalinaMandalina
4466 silver badges23 bronze badges
3 Answers
Reset to default 8You don't need all those checks and nested loops, just map the array to the length of the genre, then take the maximum:
const longestGenre = Math.max(...array.map(movie => movie.genre.length));
Or dynamically:
const longest = (key, array) => Math.max(...array.map(it => it[key].length));
console.log(longest("genre", /*in*/ array));
You can also use reduce
in a cleaner and more pact way.
Working snippet:
var arr = [
{
"title": "Ender's Game",
"genre": "Science Fiction",
"year": 2008
},
{
"title": "Harry Potter and the Goblet of Fire",
"genre": "fantasy",
"year": 2002
},
{
"title": "Pride and Predjudice",
"genre": "romance",
"year": 1980
},
{
"title": "Pride and Predjudice",
"genre": "asdasdasd asdasdasdsa asdasda",
"year": 1980
}
]
var longest = arr.reduce(function (a, b) { return a.genre.length > b.genre.length ? a : b; }).genre.length;
console.log(longest);
You could also sort the array of object lengths in decreasing order of length
of genre
. Then return the first index of the array which will always hold the greatest length.
var array = [{"title":"Ender's Game","genre":"Science Fiction","year":2008},{"title":"Harry Potter and the Goblet of Fire","genre":"fantasy","year":2002},{"title":"Pride and Predjudice","genre":"romance","year":1980}]
function getLongest(array){
return array.map((obj) => obj.genre.length).sort((a, b)=>{
return (b - a); //sorting in descending order
})[0]; //first element will hold the longest genre
}
console.log(getLongest(array));