2

Here is my JSON array:

var planets = [{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}]

Using the text "Earth" is there a method that would return me the index location of the item Earth in my Planets array?

For example:

planets.find("Earth")
Maximillion Bartango
  • 1,533
  • 1
  • 19
  • 34
  • 1. This is a JavaScript _object_ (or more specifically, an array) not a JSON array. JSON = literally text format that would represent JS objects. 2. You can use [`findIndex`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) but it's not well supported everywhere yet. If you don't need the _index_ but the actual _object_, then just use straight `find` – VLAZ Sep 13 '16 at 12:39
  • related - http://stackoverflow.com/q/15997879/104380 – vsync Sep 13 '16 at 12:52
  • related - http://stackoverflow.com/q/10557486/104380 – vsync Sep 13 '16 at 12:52

5 Answers5

2

Plain JS: findIndex

The findIndex() method returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.

[{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}].findIndex(x => x.Name === "Earth")

If you're in IE 9+, you can however use the reduce function

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

[{
  "Name": "Mercury",
  "Temperature": "427°C",
  "Position" : 1
}, {
  "Name": "Venus",
  "Temperature": "462°C",
  "Position" : 2

}, {
  "Name": "Earth",
  "Temperature": "16°C",
  "Position" : 3
}].reduce(function (foundSoFar, x, i) { // Note no arrow funcion
  if (foundSoFar < 0 && x.Name === "Earth") {
    return i;
  } else {
    return foundSoFar;
  }
}, -1);

Alternatively, use an implementation of a library like ramda

Roger
  • 2,912
  • 2
  • 31
  • 39
1

with regular js:

function getIndexOfPlanet(name){
  for( var i in planets )
    if( planets[i].Name == name )
       return i;

}

Using Lodash utility method findIndex, it's as easy as:

var planets = [{
  "Name"        : "Mercury",
  "Temperature" : "427°C",
  "Position"    : 1
}, {
  "Name"        : "Venus",
  "Temperature" : "462°C",
  "Position"    : 2
}, {
  "Name"        : "Earth",
  "Temperature" : "16°C",
  "Position"    : 3
}]

function getIndexOfPlanet(name){
  return _.findIndex(planets, { 'Name':name});
}

console.log( getIndexOfPlanet('Earth') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
vsync
  • 118,978
  • 58
  • 307
  • 400
1

You can use Array#find index of the current element being processed in the array:

var planets = [{"Name": "Mercury","Temperature": "427°C","Position": 1}, {"Name": "Venus","Temperature": "462°C","Position": 2}, {"Name": "Earth","Temperature": "16°C","Position": 3}],
    earthIndex = -1,
    earth = planets.find(function(item, index) {
        if (item.Name === 'Earth') {
            earthIndex = index;
            return true;
        }
        return false;
    });

console.log('Earth index is:', earthIndex);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • 1
    This is going to be more widely supported than `findIndex`. I suppose the only change I'd make is to return `-1` if the element is not found as that would make it behave more like a `findIndex` function but that might or might not be relevant for different use cases. – VLAZ Sep 13 '16 at 12:47
1

Try this:

var index = -1;
var val = 'Earth';
var filteredObj = planets.find(function(item, i){
  if(item.Name === val){
    index = i;
    return i;
  }
});

console.log(index, filteredObj);
Simo
  • 431
  • 1
  • 7
  • 20
1

var locationIndex; planets.forEach(function(ele,ind){if(ele.Name === "Earth") locationIndex = ind;}); console.log(locationIndex);

find or findIndex may not support some browsers like IE..

AB Udhay
  • 643
  • 4
  • 9