简体   繁体   English

为什么if / else语句在Object.assign()和.map中不起作用?

[英]Why doesn't an if/else statement work in Object.assign() and .map?

I'm learning about javascript objects and trying to wrap my head around prototype.map. 我正在学习有关javascript对象的知识,并试图将自己的头缠在prototype.map周围。 The goal is to create a new array filled with objects (using the below array filled with objects) without modifying the original objects. 目标是创建一个充满对象的新数组(使用下面的充满对象的数组)而不修改原始对象。 If the character's name is in the badGuys array then their status will be 'evil' otherwise their status will be 'good'. 如果角色的名称在badGuys数组中,则其状态将为“邪恶”,否则其状态将为“良好”。

These were the two solutions I came up with: 这些是我想出的两个解决方案:

 const badGuys = ['Joker', 'Two Face', 'Riddler', 'Bane']; const characters = [ { name: 'Flash', status: null }, { name: 'Joker', status: null }, { name: 'Batman', status: null }, { name: 'Superman', status: null }, { name: 'Two Face', status: null }, { name: 'Bane', status: null }, { name: 'Riddler', status: null }, ]; //Works: const sortedCharacters = characters.map(function (character) { return Object.assign({}, character, { status: badGuys.includes(character.name) ? "evil" : "good" }); }); //Doesn't work: const sortedCharacters = characters.map(function (character) { return Object.assign({}, character, { status: if (badGuys.includes(character.name)) { "evil" } else { "good" } }); }); 

Using the ternary operator works but an if/else statement does not. 使用三元运算符有效,但if / else语句无效。 Why? 为什么?

The important difference between if-else and the ternary operator is that in Javascript the if-else statement is a statement and the ternary operator is an expression . if-else和三元运算符之间的重要区别在于,在Javascript中,if-else语句是语句 ,而三元运算符是表达式

This means that the if-else construct does not have a value whereas the ternary does. 这意味着if-else构造没有值,而三元构造则没有。 This means that you cannot put the if-else statement in a position where a value is required. 这意味着您不能将if-else语句放置在需要值的位置。

This happens because it is not a valid syntax. 发生这种情况是因为它不是有效的语法。 The correct one is at the following snippet. 正确的代码如下。

Why the ternary operator works? 为什么三元运算符起作用?

Because the epxression before the ternary operator is evaluated and if it's true the value of status is evil. 因为对三元运算符之前的epxression进行了评估,并且如果为true,则status的值是邪恶的。 Otherwise is good. 否则是好的。

status: badGuys.includes(character.name) ? "evil" : "good"

At the following snippet we have declared a function that returns that you want. 在以下代码段中,我们声明了一个返回所需函数的函数。

 const badGuys = ['Joker', 'Two Face', 'Riddler', 'Bane']; const characters = [ { name: 'Flash', status: null }, { name: 'Joker', status: null }, { name: 'Batman', status: null }, { name: 'Superman', status: null }, { name: 'Two Face', status: null }, { name: 'Bane', status: null }, { name: 'Riddler', status: null }, ]; const sortedCharacters = characters.map(function (character) { return Object.assign({}, character, { status: function(){ if (badGuys.includes(character.name)) { return "evil" } else { return "good" } } }); }); 

But this is a quite different thing. 但这是完全不同的事情。 You are using a function in this case. 在这种情况下,您正在使用一个函数。 I would really like to know why the normal if condition doesn't work as expected 我真的很想知道为什么正常的条件不能按预期工作

As you can read from the Language documentation 如您从语言文档中所

An Object is logically a collection of properties. 从逻辑上讲,对象是属性的集合。 Each property is either a data property, or an accessor property: 每个属性都是数据属性或访问器属性:

  • A data property associates a key value with an ECMAScript language value and a set of Boolean attributes. 数据属性将键值与ECMAScript语言值和一组布尔属性相关联。
  • An accessor property associates a key value with one or two accessor functions, and a set of Boolean attributes. 访问器属性将键值与一个或两个访问器函数以及一组布尔属性相关联。 The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property. 访问器函数用于存储或检索与属性关联的ECMAScript语言值。

A data property associates a key value with the attributes 数据属性将键值与属性相关联

  • [[Value]] [[值]]
  • [[Writable]] [[可写]]
  • [[Enumerable]] [[可枚举]]
  • [[Configurable]] [[可配置]]

What is the value ? 有什么价值?

The value retrieved by a get access of the property. 通过属性的get访问获取的值。

Which is it's Domain? 哪个是域?

Any ECMAScript language type 任何ECMAScript语言类型

Which are the ECMAScript language types? 哪种ECMAScript语言类型?

  • Undefined Type 未定义类型
  • Null Type 空类型
  • Boolean Type 布尔型
  • String Type 字符串类型
  • Symbol Type 符号类型
  • Number Type 号码类型
  • Object Type 对象类型

That being said, an if/else statement cannot be stand as a value related to a key of an object. 话虽这么说,if / else语句不能作为与对象的键相关的值。

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

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