从 JSON 值实现计数器



我正在编写一个接收以下json的云函数

{
    "data":{
        "preference1" : "Food",
        "preference2" : "Travel",
        "preference3" : "Fashion"
}

用户从 10 个首选项列表中选择前 3 个首选项,我想实现计数器,以便我可以跟踪哪些是选择最多的。

我正在考虑以这样一种方式实现计数器,即我将声明变量,然后执行一系列 if 语句以添加 + 1(如果它与字符串匹配(。

var food = 0 
var travel = 0 
var fashion = 0 
if (data.preference1 == 'Food') { 
  var food = 1 
}
if (data.preference1 == 'Travel') { 
  var travel = 1 
}

。等等。

最终要做...

countDoc.ref.update({
  food_count: countDoc.ref.data().food_count + food,
  travel_count: countDoc.ref.data().travel_count + travel,
  fashion_count: countDoc.ref.data().fashion_count + fashion
})

这会给我带来想要的结果,但我知道必须有一种更清洁的方法。我不知道如何搜索此问题或尝试哪种方法。

如果我能指出正确的方向,那就太好了。

谢谢。

首先,这是一个无效的JSON——尾随逗号("Fashion"之后的逗号,没有其他元素(在JSON中是非法的。假设这是固定的...

虽然可能有正当理由说明这是 JSON 的形式,但大多数人可能更喜欢这种形式:

{ "data": { "preferences": ["Food", "Travel", "Fashion"] } }

使用数组而不是在其键中包含索引的对象。但是假设你有你说的话...

// initialise the counter
const counter = {};
// count the data
Object.values(data).forEach(
  pref => counter[pref] ? counter[pref]++ : counter[pref] = 1
);

在这里,counter["Food"]等将包含正确的计数。

您可以使用地图:

const map = new Map()
// For each data item
Object.values(data).forEach((preference) => {
  const count = (map.get(preference) || 0) + 1
  map.set(preference, count)
})

假设您有一个数据项数组:

const items = [
  {
    preference1: 'Food',
    preference2: 'Travel',
    preference3: 'Fashion',
  },
  {
    preference1: 'Something',
    preference2: 'Travel',
    preference3: 'Fashion',
  },
  {
    preference1: 'Food',
    preference2: 'Something Else',
    preference3: 'Fashion',
  },
]
const map = new Map()
// For each data item
items.forEach((item) => {
  Object.values(item).forEach((preference) => {
    const count = (map.get(preference) || 0) + 1
    map.set(preference, count)
  })
})
console.log(map)

输出:

Map {
  'Food' => 2,
  'Travel' => 2,
  'Fashion' => 3,
  'Something' => 1,
  'Something Else' => 1 }

如果我正在寻找前三名,我会做这样的事情:

function Counter(title, start){
  this.title = title; this.count = start || 0;
  this.add = function(){
    this.count++;
    return this;
  }
  this.sub = function(){
    this.count--;
    return this;
  }
}
var pants = new Counter('pants'), people = new Counter('people'), boxes = new Counter('boxes'), shirts = new Counter('shirts'), food = new Counter('food'), trees = new Counter('trees'), water = new Counter('water'), rain = new Counter('rain'), sun = new Counter('sun'), animals = new Counter('animals'), shelter = new Counter('shelter');
pants.add().add().add(); people.add(); boxes.add().add(); shirts.add().add().add(); food.add().add().add().add().add(); trees.add().add(); water.add().add().add().add().add(); rain.add().add(); sun.add(); animals.add(); shelter.add().add().add().add().add();
var counts = [pants, people, boxes, shirts, food, trees, water, rain, sun, animals, shelter];
counts.sort(function(a, b){
  return b.count-a.count;
});
var topThree = counts.slice(0, 3);
topThree.forEach(function(o){
  console.log(o.title);
});

请注意,您可以动态地counterInstance.add()单击 DOM,以获得所需的结果。

最新更新