简体   繁体   English

OR运算符如何在javascript或打字稿中的find函数中工作?

[英]How does OR operator work in find function in javascript or typescript?

If I have an array of objects: 如果我有一个对象数组:

myArray = [
  {id:123, name:"abc"},
  {id:234, name:"xyz"},
  {id:345, name:"pqr"}
]

someValue = 123
someOtherObj = {id: 234}

let matchedObj = this.myArray.find(object=> object.id === somevalue || object.id === someOtherObj.id) 
console.log(matchedObj)

Would the output be 123 every time, or it could be 123 or 234? 每次输出是123,还是123或234? Can you explain how this works? 您能解释一下这是如何工作的吗?

find finds the first matching entry in the array. find查找数组中的第一个匹配条目。 condition1 || condition2 condition1 || condition2 is true if either condition1 or condition2 is true. condition1 || condition2为真,如果任何 condition1 condition2是真实的。 (More specifically: || evaluates its left-hand operand first, and if that result is truthy, takes that truthy value as its result; if the left-hand result is falsy, || evaluates its right-hand operand and takes that value as its result.) (更具体地说: ||评估其左侧操作数,如果结果为真,则以该真实值作为结果;如果左侧结果为伪造, ||评估其右侧操作数并获取该值结果)。

So with that sample data, it will always match the first entry, because it matches the condition and it's before the second entry (which also matches the condition). 因此,对于该样本数据,它将始终与第一个条目匹配,因为它与条件匹配并且位于第二个条目之前(也与条件匹配)。 But if the entries were in a different order, where the id: 234 were before the id: 123 , it would find that one instead: 但是,如果条目的顺序不同,则id: 234id: 123之前,它将找到那个:

 const someValue = 123; const someOtherObj = {id: 234}; function match(array) { console.log(array.find(object => object.id === someValue || object.id === someOtherObj.id)); } match([ {id:123, name:"abc"}, // Finds this one {id:234, name:"xyz"}, {id:345, name:"pqr"} ]); match([ {id:345, name:"pqr"}, {id:234, name:"xyz"}, // Finds this one {id:123, name:"abc"} ]); 

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

相关问题 Typescript“+”运算符如何与泛型和函数一起使用? - How does Typescript "+" operator work with generics and functions? TypeScript / React在currentvalue =函数接口的地方,扫描运算符如何工作? - TypeScript/React how does scan operator work where currentvalue = function interface? 如何将 JavaScript 函数转换为在打字稿中工作 - How to convert JavaScript function to work in typescript 这个 TypeScript 函数参数是如何工作的 - How does this TypeScript function parameter work Javascript/Typescript 中类的交叉引用如何工作? - How does cross referencing of classes in Javascript/Typescript work? Typescript 箭头函数中的对象文字参数声明如何工作? - How does an object literal parameter declaration in a Typescript arrow function work? 如何在 typescript 中使用扩展运算符作为 function 的参数? - How to use spread operator in typescript as argument to a function? 基于 Typescript 的 Reactjs 中的三元运算符无法按预期工作 - Ternary operator in Typescript based Reactjs does not work as I expected 为什么无效合并运算符在 typescript 中不能用作类型保护? - Why does the nullish coalescing operator not work as a typeguard in typescript? Typescript --allowJs 如何工作? - How does Typescript --allowJs work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM