score:2

Accepted answer

Given your data structure, an easy approach is just filtering the data array inside makeScatter function.

function makeScatter(country) {
    var filteredData = data.filter(d => d.country === country);
}

Here is a demo, check the console:

var data = [{
  "country": "Abkhazia",
  "1995": null,
  "1996": null,
  "1997": null,
  "1998": null,
  "1999": null,
  "2000": null
}, {
  "country": "Afghanistan",
  "1995": 49.4,
  "1996": 49.7,
  "1997": 49.5,
  "1998": 48.6,
  "1999": 50,
  "2000": 50.1,
  "2001": 50.4
}, {
  "country": "Angola",
  "1995": 59.4,
  "1996": 59.7,
  "1997": 39.5,
  "1998": 58.6,
  "1999": 60,
  "2000": 60.1,
  "2001": 60.4
}];

function makeScatter(country) {
  var filteredData = data.filter(d => d.country === country);
  console.log(filteredData);
}

makeScatter("Afghanistan")

However, there is a potential problem here: in D3, the "enter" selection has as many elements as the array you pass to the data() function. Right now, this filteredData array has just one object, meaning that you'll have only one element in the enter selection.

Thus, my advice is this: after filtering your country, transform that huge object in an array of objects (each object having a year and an expectancy property), here named countryData:

function makeScatter(country) {
    var filteredData = data.filter(d => d.country === country);
    countryData = [];
    for (var prop in filteredData[0]) {
        countryData.push({
            year: +prop,
            expect: filteredData[0][prop]
        })
    }
}

Here is the demo:

var data = [{
  "country": "Abkhazia",
  "1995": null,
  "1996": null,
  "1997": null,
  "1998": null,
  "1999": null,
  "2000": null
}, {
  "country": "Afghanistan",
  "1995": 49.4,
  "1996": 49.7,
  "1997": 49.5,
  "1998": 48.6,
  "1999": 50,
  "2000": 50.1,
  "2001": 50.4
}, {
  "country": "Angola",
  "1995": 59.4,
  "1996": 59.7,
  "1997": 39.5,
  "1998": 58.6,
  "1999": 60,
  "2000": 60.1,
  "2001": 60.4
}];

function makeScatter(country) {
  var filteredData = data.filter(d => d.country === country);
  countryData = [];
  for (var prop in filteredData[0]) {
    countryData.push({
      year: +prop,
      expect: filteredData[0][prop]
    })
  }
  console.log(countryData)
}

makeScatter("Afghanistan")


Related Query

More Query from same tag