计算特定的对象属性值(如果它们匹配)

时间:2019-02-26 21:11:09

标签: javascript

我想计算匹配对象属性值的数量。更具体地讲,如果我的结果object.place_name相同,则将其保留在下一项,将其全部累加,然后使用res.render将该结果传递到前端

当前对象

     results = [ { id: 'country.8605848117814600',
    type: 'Feature',
    place_type: [ 'country'
    ],
    relevance: 1,
    properties: { short_code: 'gb', wikidata: 'Q145'
    },
    text: 'United Kingdom',
    place_name: 'United Kingdom',
    bbox: [
        -8.718659,
        49.802665,
        1.867399,
        60.945453
    ],
    center: [
        -2,
        54
    ],
    geometry: { type: 'Point', coordinates: [Array
        ]
    }
},
{ id: 'country.12399313490269000',
    type: 'Feature',
    place_type: [ 'country'
    ],
    relevance: 1,
    properties: { short_code: 'dk', wikidata: 'Q35'
    },
    text: 'Denmark',
    place_name: 'Denmark',
    bbox: [
        7.970276,
        54.3991486,
        15.253716,
        57.9322004
    ],
    center: [
        10,
        56
    ],
    geometry: { type: 'Point', coordinates: [Array
        ]
    }
},
{ id: 'country.12399313490269000',
    type: 'Feature',
    place_type: [ 'country'
    ],
    relevance: 1,
    properties: { short_code: 'dk', wikidata: 'Q35'
    },
    text: 'Denmark',
    place_name: 'Denmark',
    bbox: [
        7.970276,
        54.3991486,
        15.253716,
        57.9322004
    ],
    center: [
        10,
        56
    ],
    geometry: { type: 'Point', coordinates: [Array
        ]
    }
},
{ id: 'country.8605848117814600',
    type: 'Feature',
    place_type: [ 'country'
    ],
    relevance: 1,
    properties: { short_code: 'gb', wikidata: 'Q145'
    },
    text: 'United Kingdom',
    place_name: 'United Kingdom',
    bbox: [
        -8.718659,
        49.802665,
        1.867399,
        60.945453
    ],
    center: [
        -2,
        54
    ],
    geometry: { type: 'Point', coordinates: [Array
        ]
    }
}
]

我要运行的代码

    const promises = results.map(result =>

      Promise.all([
        geoPromise(result.Country_Name),
        geoPromise(result.Organisation_Name),
        result.Output_Title_Name
      ])

    );



    Promise.all(promises)
      .then((values) => {
        let results = values.map(elmt => elmt[0]);
        console.log(results)
        let businesses = values.map(elmt => elmt[1]);


        let names = values.map(elmt => elmt[2]);

         place_name = ['Denmark', 'United Kingdom']
  count = results.reduce((s, o) => s + (o.place_name === place_name[0]), 0);

  countObject = {place_name:'',
                 count:''}

                 countObject.place_name = place_name[0]
                 countObject.count = count
  console.log(countObject)

        res.render('layouts/layout', {
          results: JSON.stringify(results),
          businesses: JSON.stringify(businesses),
          names: JSON.stringify(names),
          resultCount: resultCount
        });
      })

但是,我的resultCount仅返回一个空数组。有人可以描述一种更好的方式来做我想做的事吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Array#reduce并计算条件是否产生true并带有对象的值和给定值。

var results = [{ id: 'country.8605848117814600', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'gb', wikidata: 'Q145' }, text: 'United Kingdom', place_name: 'United Kingdom', bbox: [-8.718659, 49.802665, 1.867399, 60.945453], center: [-2, 54], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.12399313490269000', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'dk', wikidata: 'Q35' }, text: 'Denmark', place_name: 'Denmark', bbox: [7.970276, 54.3991486, 15.253716, 57.9322004], center: [10, 56], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.12399313490269000', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'dk', wikidata: 'Q35' }, text: 'Denmark', place_name: 'Denmark', bbox: [7.970276, 54.3991486, 15.253716, 57.9322004], center: [10, 56], geometry: { type: 'Point', coordinates: [Array] } }, { id: 'country.8605848117814600', type: 'Feature', place_type: ['country'], relevance: 1, properties: { short_code: 'gb', wikidata: 'Q145' }, text: 'United Kingdom', place_name: 'United Kingdom', bbox: [-8.718659, 49.802665, 1.867399, 60.945453], center: [-2, 54], geometry: { type: 'Point', coordinates: [Array] } }];
    place_name = 'Denmark',
    count = results.reduce((s, o) => s + (o.place_name === place_name), 0);

console.log(count);