计算对象中同一键内的多个唯一值

时间:2018-08-26 07:22:44

标签: javascript ajax fetch

我试图跟踪同一键中四个特定字符串值出现的次数。

问题是,我很难添加多个计数器。第一个计数器起作用并跟踪第一个条件,但是if / else语句中的下一个条件会阻止初始条件和此后的每个条件正确加总。数据来自包含多个URL的PIVOT

这是我的代码:

Promise.all

const urls = [ 'https://api.github.com/users/TylerP33/repos?page=1', 'https://api.github.com/users/TylerP33/repos?page=2', 'https://api.github.com/users/TylerP33/repos?page=3', 'https://api.github.com/users/TylerP33/repos?page=4', 'https://api.github.com/users/TylerP33/repos?page=5', 'https://api.github.com/users/TylerP33/repos?page=6', 'https://api.github.com/users/TylerP33/repos?page=7', 'https://api.github.com/users/TylerP33/repos?page=8', 'https://api.github.com/users/TylerP33/repos?page=9', 'https://api.github.com/users/TylerP33/repos?page=10', 'https://api.github.com/users/TylerP33/repos?page=11', 'https://api.github.com/users/TylerP33/repos?page=12' ] function getLanguages() { return Promise.all(urls.map(url => fetch(`${url}`) .then(response => response.json()) .then(obj => obj.forEach(function(val) { var rubyCounter = 0 var cssCounter = 0 var htmlCounter = 0 var jsCounter = 0 if (val.language === "Ruby") { rubyCounter++; console.log(rubyCounter); } }))) ) } getLanguages(); 将返回rubyCounter,这是正确的金额,但是如果我再添加一些条件,就像通过同一键运行的true / false条件会抛出所有错误。我可能会缺少一些明显的东西,但我很好奇看到你们的想法。

谢谢。

2 个答案:

答案 0 :(得分:1)

对于此代码,rubyCounter如果被记录,将始终为1,因为这些计数器始终在回调中初始化。

您需要将这些计数器的定义移至getLanguages的根。

而不是为每个计数器使用多重变量,我将使用对象以及要计数的每种语言都具有一个属性。

const urls = [
  'https://api.github.com/users/TylerP33/repos?page=1',
  'https://api.github.com/users/TylerP33/repos?page=2',
  'https://api.github.com/users/TylerP33/repos?page=3',
  'https://api.github.com/users/TylerP33/repos?page=4',
  'https://api.github.com/users/TylerP33/repos?page=5',
  'https://api.github.com/users/TylerP33/repos?page=6',
  'https://api.github.com/users/TylerP33/repos?page=7',
  'https://api.github.com/users/TylerP33/repos?page=8',
  'https://api.github.com/users/TylerP33/repos?page=9',
  'https://api.github.com/users/TylerP33/repos?page=10',
  'https://api.github.com/users/TylerP33/repos?page=11',
  'https://api.github.com/users/TylerP33/repos?page=12'
]


function getLanguages() {
  let counter = {
    ruby: 0,
    html: 0
  }

  return Promise.all(urls.map(url =>
      fetch(`${url}`)
      .then(response => response.json())
      .then(obj => obj.forEach(function(val) {
        /*var rubyCounter = 0
        var cssCounter = 0
        var htmlCounter = 0
        var jsCounter = 0*/
        if (val.language === "Ruby") {
          counter.ruby++;
        } else if (val.language === "HTML") {
          counter.html++;
        }

      }))))
    .then(() => {
      console.dir(counter)
    })
}

getLanguages();

如果您想表达所有语言,那就更简单了:

const urls = [
  'https://api.github.com/users/TylerP33/repos?page=1',
  'https://api.github.com/users/TylerP33/repos?page=2',
  'https://api.github.com/users/TylerP33/repos?page=3',
  'https://api.github.com/users/TylerP33/repos?page=4',
  'https://api.github.com/users/TylerP33/repos?page=5',
  'https://api.github.com/users/TylerP33/repos?page=6',
  'https://api.github.com/users/TylerP33/repos?page=7',
  'https://api.github.com/users/TylerP33/repos?page=8',
  'https://api.github.com/users/TylerP33/repos?page=9',
  'https://api.github.com/users/TylerP33/repos?page=10',
  'https://api.github.com/users/TylerP33/repos?page=11',
  'https://api.github.com/users/TylerP33/repos?page=12'
]


function getLanguages() {
  let counter = {}

  return Promise.all(urls.map(url =>
      fetch(`${url}`)
      .then(response => response.json())
      .then(obj => obj.forEach(function(val) {
        if( val.language ) {
          counter[val.language] = counter[val.language] || 0
          counter[val.language]++
        }
      }))))
    .then(() => {
      console.dir(counter)
    })
}

getLanguages();

答案 1 :(得分:1)

请检查它是否对您有用:

const urls = [
            'https://api.github.com/users/TylerP33/repos?page=1',
            'https://api.github.com/users/TylerP33/repos?page=2'
        ]

        var counters = {};
        function getLanguages() {
            return Promise.all(urls.map(url => {
                fetch(`${url}`)
                    .then(response => response.json())
                    .then(json => json.forEach(function (val) {
                        if (!counters[val.language])
                            counters[val.language] = 0;

                        counters[val.language]++;

                    }))
                    .then(() => {
                        console.log(counters);
                    });
            }));
        }

        getLanguages();