最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find longest string in array of objects by string property value - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论