如何使用javascript识别数组中的自定义值?

如何使用javascript识别数组中的自定义值?,javascript,arrays,Javascript,Arrays,我做了一项调查,调查对象有一个选择题,比如说:你最喜欢的水果是什么?他们可以从列表中任意选择,但如果他们想添加未列出的水果,也可以引入自定义值。 该列表有以下选项:苹果、梨、香蕉、草莓和自定义 我需要用这些信息制作一张图表,统计有多少人选择了每种水果。但我会将所有自定义值添加到一个名为custom的值中。 假设我得到了一个这样的数组: [ { "Person": "Person1", "Fruits": [ "apple", "banana", "peach" ] },

我做了一项调查,调查对象有一个选择题,比如说:你最喜欢的水果是什么?他们可以从列表中任意选择,但如果他们想添加未列出的水果,也可以引入自定义值。 该列表有以下选项:苹果、梨、香蕉、草莓和自定义

我需要用这些信息制作一张图表,统计有多少人选择了每种水果。但我会将所有自定义值添加到一个名为custom的值中。 假设我得到了一个这样的数组:

[
  {
    "Person": "Person1",
    "Fruits": [ "apple", "banana", "peach" ]
  },
  {
    "Person": "Person2",
    "Fruits": [ "apple", "pear", "strawberry", "coconut" ]
  },
  {
    "Person": "Person3",
    "Fruits": [ "pear", "strawberry" ]
  },
  {
    "Person": "Person4",
    "Fruits": [ "strawberry", "orange" ]
  }
]
为了计算已经列出的水果,我开始使用includes,但IE不支持它。所以我用的是我找到的代码

我的代码: 我为数组中的每个元素创建了一个循环,如果它包含某个单词,它将存储在对应的数组中:

//I am calling the info with AngularJS don't know if is important to know that
//Declare empty arrays for each fruit
var dataResults = $scope.items, //<---- this is the array with the info, the one I showed before..
    apple = [],
    pear = [],
    banana = [],
    strawberry = [],
    custom = [];

//function to read if an array includes a certain value
function includes(container, value) {
    var returnValue = false;
    var pos = container.indexOf(value);
    if (pos >= 0) {
        returnValue = true;
    }
    return returnValue;
}

//Loop the survey results
for(var i=0; i<dataResults.length; i++) {
    var currentItem = dataResults[i];
    //I push the name of the person if he/she chose apple
    if(includes(currentItem.Fruits, "apple")){
        apple.push(currentItem.Person);
    }
    //I push the name of the person if he/she chose pear
    if(includes(currentItem.Fruits, "pear")){
        pear.push(currentItem.Person);
    }
    //I push the name of the person if he/she chose banana
    if(includes(currentItem.Fruits, "banana")){
        banana.push(currentItem.Person);
    }
    //I push the name of the person if he/she chose strawberry
    if(includes(currentItem.Fruits, "strawberry")){
        strawberry.push(currentItem.Person);
    }
};
//now I wanna see the results in the console for each array
console.log("apple: " + apple.length);
console.log("pear: " + pear.length);
console.log("banana: " + banana.length);
console.log("strawberry: " + strawberry.length);
但现在我不知道有多少人输入了自定义值。他们写什么并不重要,因为可以有数百个不同的值,但我需要将它们全部存储为自定义值。 如何计算列表中未显示的值?
请帮忙。

在我看来,这是一个很好的声明选择

您还可以删除使用包含的必要性

例如:

//Loop the survey results
var currentItem, f, max;
for(var i=0; i<dataResults.length; i++) {
    currentItem = dataResults[i];
    max = currentItem.Fruits.length;
    for(f=0;f<max;f++) {
        switch(currentItem.Fruits[f]) {
            case "apple" :
                apple.push(currentItem.Person);
                break;
            case "pear" :
                pear.push(currentItem.Person);
                break;
            case "banana" :
                banana.push(currentItem.Person);
                break;
            case "strawberry" :
                strawberry.push(currentItem.Person);
                break;
            default :
                custom.push(currentItem.Person);
        }
    }
}

您可以检查当前项目中的每个水果,看看它是否不在标准列表中。如果不是,则必须是定制水果:

let standardList = ["apple", "pear", "banana", "strawberry"];
for(let fruit of currentItem.Fruits){
  if(!includes(standardList, fruit)){
    custom.push(currentItem.Person);
  }
}
以下是在工作代码段中应用的这一概念:

//属性为列表的对象,以收集喜欢每种水果的人 变量列表={ 苹果[],梨[],香蕉[],草莓[],定制:[] }, //入境调查结果 数据结果=[ {人:人1,水果:[苹果,香蕉,桃子]}, {人:人2,水果:[苹果,梨,草莓,椰子]}, {人:人3,水果:[梨,草莓]}, {人:人4,水果:[草莓,橘子]} ], //标准非定制水果名称列表 标准列表=[苹果、梨、香蕉、草莓]; //循环浏览结果 对于var i=0;i=0{//则使用'indexOf'而不是'includes'` 列出[fruit].pushcurrentItem.Person; } //否则,水果不在标准列表中,所以将此人推到自定义列表中 否则{ //假设每个当前项目不超过一个自定义水果 列出[custom].pushcurrentItem.Person; } } }; //对于列表对象中的每个列表,打印其名称和长度 Object.keyslists.forEachfunctionname{ console.log`${name}:${lists[name].length}`;
}有关Array.prototype.includes的建议polyfill,请参阅不知道为什么我没有考虑使用switch,它更简单而且工作完美。。