0

I have a very large array of JSON objects (about 4000 objects). The value of one of the keys is a string and I want to convert it to an integer so I can iterate the array and pull out the object with the largest value for that key. Here is a sample of the array:

[
  {
    "id": "34567",
    "title": "Some title",
    "likes": "5",
    "comments": "7",
    "views": "3"
  },
  {
    "id": "34568",
    "title": "Some title 2",
    "likes": "4",
    "comments": "7",
    "views": "3"
  },
  {
    "id": "34569",
    "title": "Some title 3",
    "likes": "3",
    "comments": "7",
    "views": "3"
  },
  {
    "id": "34560",
    "title": "Some title 4",
    "likes": "2",
    "comments": "7",
    "views": "3"
  }
]

What I'm trying to do is iterate the array, convert the 'likes' value to an integer and then pull out the object with the largest value for 'likes'.

4
  • Did you try to write anything? 4000 isn't that large, and your process seems straightforward. Commented Nov 3, 2016 at 13:03
  • Try Lodash third party script to handle these kind of scenarios, lodash.com/docs Commented Nov 3, 2016 at 13:03
  • You don't need lodash. Just loop over the object, parseInt the likes property and store the index of the highest like number. Commented Nov 3, 2016 at 13:06
  • 1
    Lodash is just a suggestion, if you want to write less code and more features. I believe .maxBy(array, [iteratee=.identity]) will solve your current issue. Use it only if you have many operations/manipulations to be done on that JSON which is basically a collection of objects. Commented Nov 3, 2016 at 13:10

2 Answers 2

2

This is just simple javascript, do something like this:

if (myArray.length) {

    var heighest = myArray[0];
    for (var i = 1; i < myArray.length; i++) {
        var likes = parseInt(myArray[i].likes);
        if (likes > heighest.likes) {
            heighest = myArray[i];
        }
    }

    console.log(heighest); // logs object with id 34567
} else {
    console.log("No items in array");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Check this solution. Store the likes value in a temporary variable and replace it if a higher value is found also by replacing the object with the current one.

var items = [{
  "id": "34567",
  "title": "Some title",
  "likes": "5",
  "comments": "7",
  "views": "3"
}, {
  "id": "34568",
  "title": "Some title 2",
  "likes": "4",
  "comments": "7",
  "views": "3"
}, {
  "id": "34569",
  "title": "Some title 3",
  "likes": "3",
  "comments": "7",
  "views": "3"
}, {
  "id": "34560",
  "title": "Some title 4",
  "likes": "2",
  "comments": "7",
  "views": "3"
}];

var best;
var max = 0;
for (var i = 0; i < items.length; i++) {
  var likes = parseInt(items[i].likes);
  if (likes > max) {
    max = likes;
    best = items[i];
  }
}

console.log(best);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.