简体   繁体   English

三元条件一次有效,但不能再一次 javascript

[英]Ternary conditional works one time but not another javascript

First of all this does not work for me, And I explain it so that they do not close the question, I am making two more or less equal algorithms with ternary conditional, simply one is an array of objects and the other an array of numbers, and with the numbers I don't have that problem, that's why What do I think is a problem with the code: When should I use a return statement in ES6 arrow functions首先,这对我不起作用,我解释一下,这样他们就不会解决问题,我正在制作两个或多或少相等的三元条件算法,一个是对象数组,另一个是数字数组,并且对于数字我没有那个问题,这就是为什么我认为代码有问题: 何时应该在 ES6 箭头函数中使用 return 语句

I'm new to the world of javascript and when doing a reduce for an array, I want to try to count the number of times the age is equal in an array of objects whose properties are the ages.我是 javascript 世界的新手,在对数组进行归约时,我想尝试计算属性为年龄的对象数组中年龄相等的次数。

However when I do a ternary conditional I receive that the output is undefined.但是,当我执行三元条件时,我收到 output 未定义。

but when doing a normal if with an else if you can do the count: Code: const numbers = [ {age: 4, age2: 4}, {age: 5, age2: 6}, {age: 88, age2: 99}, {age:14, age2:14}, {age: 2, age2: 2} ];但是当用一个正常的 if 做一个 else if 你可以做计数时: Code: const numbers = [ {age: 4, age2: 4}, {age: 5, age2: 6}, {age: 88, age2: 99 },{年龄:14,年龄2:14},{年龄:2,年龄2:2}];

const getCount = ( objects ) => objects.reduce((acc, el) => {
    if ( el.age === el.age2 ) {
        return acc += 1;
    }
    else {
        return acc;
    }
}, 0);

const b = getCount(numbers)
console.log(b)

Output: Output:

3

But with this Code:但是使用此代码:

const numbers = [ {age: 4, age2: 4}, {age: 5, age2: 6}, {age: 88, age2: 99}, {age:14, age2:14}, {age: 2, age2: 2} ];

    const getCount = ( objects ) => objects.reduce((acc, el) => {
        el.age === el.age2 ? acc += 1 : acc;
    }, 0);
    
    const b = getCount(numbers)
    console.log(b)

Output Output

undefined

However, if I don't use objects and I only do the count if some element is repeated, with the same ternary conditional, this does work.但是,如果我不使用对象并且仅在某些元素重复时才进行计数,并且使用相同的三元条件,这确实有效。

const count2 = ( objects ) => objects.reduce( (acc, el) => el === 1 ? acc += 1 : acc )
console.log(count2( [0, 1, 2, 3, 4, 5, 1, 1, 4] ))

Output Output

3

Please I do not understand what happens, what is the error请我不明白会发生什么,错误是什么

You have forgotten to return the accumulator value:您忘记返回累加器值:

const getCount = ( objects ) => objects.reduce((acc, el) => {
      return el.age === el.age2 ? acc += 1 : acc;
    }, 0);

You can also remove the { } and the keywork return您还可以删除{ }和 keywork return

const getCount = ( objects ) => objects.reduce((acc, el) => el.age === el.age2 ? acc += 1 : acc, 0)

Beside the missing return statement and using a block statement, you could simplifiy the function and just add the accumulator and the comparison value.除了缺少的return语句和使用块语句之外,您可以简化 function 并只需添加累加器和比较值。

const getCount = objects => objects.reduce((acc, el) => acc + (el.age === el.age2), 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM