Assign only if condition is true in ternary operator in JavaScript

后端 未结 9 1900
小鲜肉
小鲜肉 2020-12-02 22:08

Is it possible to do something like this in JavaScript?

max = (max < b) ? b;

In other words, assign value only if the condition is true.

9条回答
  •  -上瘾入骨i
    2020-12-02 22:36

    There was no example of ES6, we can use in this way:

    let list = [{id: "abc", name: "test1"}, {id: "xyz", name: "test2"}]
    let selectedData = {};
          list.some((exp) => {
            return (
              exp.id == "xyz" &&
              ((selectedData = exp), true)
            );
          })
    console.log(selectedData);
    

提交回复
热议问题