【问题标题】:How to assign values from one array to others when there are too many?当数组太多时如何将一个数组中的值分配给其他数组?
【发布时间】:2019-05-04 21:44:12
【问题描述】:

我显然不知道如何在标题中解释自己..
我有一个包含调查信息的数组,该数组向我提供了人们说他们使用更多的应用程序以及他们如何评估它们。
从列表中您可以选择以下应用:

  • Excel
  • 电源点
  • 访问
  • 功率比

您必须从以下方面对每个应用进行评分:

  • 可用性
  • 性能
  • 移动功能
  • 数据移动

这些功能可以被评估为“1-Poor”、“2-Average”、“3-Good”或“4-N/A”。

此外,每个人都按他们所在的大陆划分:美洲、亚洲、欧洲、非洲、大洋洲。

我的问题:
我需要按他们的大陆将人们分开。 以及他们如何评估每个应用程序:

来自美国的人,他们在“可用性”中评价为“差”Word
来自美国的人,他们在“表现”中评价为“差”Word
来自美国的人,他们在“移动能力”中评价为“差”Word
来自美国的人,他们在“数据移动”中评价为“差”Word

我需要为每个评估执行此精确循环:差、平均、好和 N/A。
还适用于每个应用程序:Word、Excel、Power Point、Access、Power Bi。
还按大陆:美洲、亚洲、欧洲、非洲、大洋洲。

这就是我的数组的样子(只是一个人的例子):

var results = [
{
    "Author": "Person 1",
    "Word Usability": "3",
    "Word Performance": "2",
    "Word Mobile capability": "1",
    "Word Data movement": "4",
    "Excel Usability": "3",
    "Excel Performance": "2",
    "Excel Mobile capability": "1",
    "Excel Data movement": "4",
    "PowerPoint Usability": "3",
    "PowerPoint Performance": "2",
    "PowerPoint Mobile capability": "1",
    "PowerPoint Data movement": "4",
    "Access Usability": "3",
    "Access Performance": "2",
    "Access Mobile capability": "1",
    "Access Data movement": "4",
    "PowerBi Usability": "3",
    "PowerBi Performance": "2",
    "PowerBi Mobile capability": "1",
    "PowerBi Data movement": "4",
    "Continent": "America"
},
...
]

我所做的是创建各种数组,然后将它们循环以按区域分隔人员,以及他们如何通过之前标记的各个方面评估每个应用程序:

<script>
//I created arrays for each continent
var america = [],
    europe = [],
    asia = [],
    africa = [],
    oceania = [];

//And also separate the count of each "evaluation" from each aspect to rate the app
var americaWord_Usability1 = 0,
    americaWord_Usability2 = 0,
    americaWord_Usability3 = 0,
    americaWord_Usability4 = 0;
var americaWord_Performance1 = 0,
    americaWord_Performance2 = 0,
    americaWord_Performance3 = 0,
    americaWord_Performance4 = 0;
...

//I make the loop
for(var i=0;i<results.length;i++) {
    var currentItem = results[i];

    //depending their continent, I send each people to their correspondent array
    if(currentItem.Continent == "America") {
        america.push(currentItem);
    }
    ...
}

//now I loop by region and send them depending their app and how they were evaluated in each aspect
for(var q=0;q<america.length;q++){
    var currentItem = america[q];

    if(currentItem["Word Usability"] == "1") {
        americaWord_Usability1++;
    } else if(currentItem["Word Usability"] == "2") {
        americaWord_Usability2++;
    } else if(currentItem["Word Usability"] == "3") {
        americaWord_Usability3++;
    } else {
        americaWord_Usability4++;
    }
    if(currentItem["Word Performance"] == "1") {
        americaWord_Performance1++;
    } else if(currentItem["Word Performance"] == "2") {
        americaWord_Performance2++;
    } else if(currentItem["Word Performance"] == "3") {
        americaWord_Performance3++;
    } else {
        americaWord_Performance4++;
    }
    ...
}
</script>

使用这种方法,这一切都变成了一场噩梦。这是很多代码,因为我需要为每个大陆制作一个数组,并为每个方面制作一个数组来评价每个应用程序!
我的实际代码有点长,要评价的方面和应用程序更多。所以我的代码变得非常长。

我的问题是:有没有更好更简单的方法来实现这一点?
提前致谢!

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    为了让事情变得更简单,您可以动态创建数组:

    var userData = [];
    
    for(var i=0;i<results.length;i++) {
        var currentItem = results[i];
    
        // If the subarray doesn't exist yet, create it
        if (userData[currentItem.Continent] == null) userData[currentItem.Continent] = [];
    
        // Push the data into the corresponding array
        userData[currentItem.Continent].push(currentItem);
    }
    

    现在您将拥有一个数组,其中包含按大陆分隔的所有用户。您可以将相同的逻辑应用到您的评分中,例如:

    // List all the possible keywords for ratings here
    var ratingKeywords = ["Word Usability", "Excel Usability"];
    
    // Empty arra for ratings
    var ratings = [];
    
    // Go through the continents
    for (var continentName in userData) {
        var continent = userData[continentName];
        // Array for continent ratings
    
        if (!ratings[continentName]) ratings[continentName] = []; 
    
        for (var u in continent) {
            var user = continent[u];
            for (var k in ratingKeywords) {
                var keyword = ratingKeywords[k];
                console.log(keyword);
    
                var rating = user[keyword];
    
                if (!ratings[continentName][keyword]) ratings[continentName][keyword] = [];
                if (!ratings[continentName][keyword][rating.toString()]) ratings[continentName][keyword][rating.toString()] = 0;
                ratings[continentName][keyword][rating.toString()]++;
            }
        }
    }
    

    这看起来有点笨拙,但您不需要硬编码任何东西。如果出现一个新大陆(由于海平面上升,这可能不会发生,请尽快吃素或减少肉类消费),此代码应该仍然有效。

    您最终会得到一个具有以下结构的数组,例如: ratings["Europe"]["Word Usability"][5]: 400

    (请注意,您可能不应该使用 for-in-loops,但是...是的)

    【讨论】:

    • 两个问题。 1:在 ratingKeywords 数组中,我必须添加每个应用程序的所有方面还是只添加 “usability” 的方面? 2:我认为keyword的部分有问题(不太确定),它在if (!ratings[region][keyword][rating])的部分抛出这个错误:Cannot read property '0' of undefined
    • 哎呀,对不起。完成答案时分心,并没有真正查看原始数据,因此我对其进行了编辑。它现在应该可以工作了。此处的 ratingKeywords 是您想要跟踪的任何内容,所以是的,在此示例中您应该添加所有内容。您还可以动态创建关键字数组。 (如let keyword = app + " " + property;
    • 哇,现在效果更好了,感谢您在此方面的帮助 :) ..只有一个小问题:当某个应用程序和大陆的某个方面没有项目时,它会将值抛出为 @ 987654328@,例如:console.log(ratings["America"]["Word Performance"][1]) 给了我 undefined 因为“美国”数组中没有“Word Performance: 1”的值...我怎样才能让它抛出“0”(零)而不是?
    • 你可以用零作为默认值来初始化分数数组。
    【解决方案2】:

    您可以改用 javascript 过滤器。喜欢关注

    ex:
       filteredListwith1 = results.filter((item) => { 
             if(item.continent === 'America' && item['Word Usability'] === '1') {
                  return item;
              });
        americaBox_Usability1 = filteredListwith1.length;
    

    同样的事情你也可以重复 2 和 3。

    【讨论】:

    • 不知道这是否真的减少了代码...我的意思是,我是否必须为每个应用程序、每个要评分的方面以及每个数字(1 2、3和4)?因为那时我认为我在循环中的if 语句实际上更短。
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多