计算数组中的元素并将其添加到对象中 [英] Counting elements in an array and adding them into an object

查看:135
本文介绍了计算数组中的元素并将其添加到对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我研究了这个问题,但在这里找不到答案,因此希望这不是重复的问题。我还尝试不具体说明以便自己解决。

Okay I researched this one and couldn't find an answer here so hopefully this isn't a duplicate. I'm also trying not to be specific in order to figure this out on my own.

var arr = ['cat','car','cat','dog','car','dog']

function orgNums(input) {
    var count = 0;
    var obj = {};
    for (var i = 0; i < input.length; i++) {
        if (input[i] === 'cat')
        obj['cat'] = count++;
    }
    return obj;
}

我希望它返回 {cat:2} 但我得到了 {cat:1}

I want it to return {cat:2} but I'm getting {cat:1}

最终我希望它return {cat:2,car:1,dog:2,gerbil:1}

Eventually I want it to return {cat:2, car:1, dog:2, gerbil:1}

所以我尝试使用 obj [cat] = ++ count ,我得到了想要的答案,但是当我尝试第二次if语句时: if input [i ] ==='dog',obj [dog] = ++ count 我得到 {cat:2,dog:4} 。我注意到它正在使用已经在 count 处的数值,然后将其移至2以对cat进行计数,然后将其移至4,将狗计数为2并加2对它。如何防止这种情况发生,以使 count 每次都从0重新开始?

So I tried using obj[cat] = ++count and I'm getting the answer I want, but when I try a second if statement: if input[i] === 'dog', obj[dog] = ++countI get {cat:2, dog:4}. I noticed that it's taking what count already is at, 0, then moves it to 2 to count cat, then moves it to 4, taking the dog count of 2 and adding 2 to it. How do I prevent that from happening so that count restarts at 0 each time?

编辑:
因此,它的效果很不错
var arr = ['cat','car','cat','dog','car','dog']

So this works beautifully var arr = ['cat', 'car', 'cat', 'dog', 'car', 'dog']

function orgNums(input) {
    var obj = {};

    for (var i = 0; i < input.length; i++) {
        obj[input[i]] = obj[input[i]] || 0;
        obj[input[i]]++;
    }
    return obj;
}

console.log(orgNums(arr));

但我真正想要的最终输出是:

but the final output i actually want is:

[
{cat:1
dog:2
}
{car:2
}
]

所以我试图抛出一个if语句:

So I tried throwing in an if statement like this:

if (input[i] === 'cat'||'dog')

,但它仍将汽车扔到对象中。我将尝试找出数组中的多个对象。再次感谢!

but it's still throwing car into the object. I'll try to figure out the multiple objects in the array. Thanks again!

推荐答案

count 的赋值用于由于后缀增量 计数++ 。这将为您提供计数器的值,并在以后增加。如果您使用前缀递增 ++ count ,则会获得分配的递增值。

The assignment of count, which is used in the loop, does not work in this case, because of the postfix increment count++. This gives you the value of the counter and increment later, If you take the prefix increment ++count, you get the incremented value assigned.


// Postfix 
var x = 3;
y = x++; // y = 3, x = 4

// Prefix
var a = 2;
b = ++a; // a = 3, b = 3


但是您可以省略变量并直接使用对象的属性进行计数。

But you can omit the variable and count directly with a property of the object. for all items in one loop.

您可以将元素用作对象的键。然后分配零(如果不存在则递增)。

You could use the element as key for the object. Then assign with zero, if not exist and increment.

var arr = ['cat', 'car', 'cat', 'dog', 'car', 'dog']

function orgNums(input) {
    var obj = {};

    for (var i = 0; i < input.length; i++) {
        obj[input[i]] = obj[input[i]] || 0;
        obj[input[i]]++;
    }
    return obj;
}

console.log(orgNums(arr));

上述内容的更紧凑版本,其中 Array#forEach

A more compact version of the above with Array#forEach

var arr = ['cat', 'car', 'cat', 'dog', 'car', 'dog']

function orgNums(input) {
    var obj = {};

    input.forEach(function (item) {
        obj[item] = (obj[item] || 0) + 1;
    });
    return obj;
}

console.log(orgNums(arr));

这篇关于计算数组中的元素并将其添加到对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆