简体   繁体   English

如何在 React JS 的 foreach 循环内显示 if 语句的值?

[英]How to show the value from an if statement inside a foreach loop in react js?

hello i am using react js, any advice and suggestions are hugely appreciated here.您好,我正在使用 React JS,在这里非常感谢任何意见和建议。 The sharedfields forEach loop does console.log the sharedFieldAlias value, if I console.log it outside of the loop it will not output the value. sharedfields forEach 循环执行 console.log sharedFieldAlias 值,如果我在循环之外进行 console.log,它不会是 output 值。 I need the sharedFieldAlias value to display in the span tag but it gives a reference error that sharedFieldAlias is not defined.我需要在 span 标记中显示 sharedFieldAlias 值,但它给出了一个引用错误,指出未定义 sharedFieldAlias。 How can I display the value in the span tag?如何显示 span 标签中的值?

sharedfields.forEach((sharedField) => {
        let sharedFieldAlias = sharedField.alias;
        if(value.shared_field_uuid == sharedField.uuid) {
          console.log(sharedFieldAlias);
        }
      });
      
      const fullLabel = (
        <div style={{ display: 'flex', alignItems: 'center' }}>
          <InputLabel label={display_name} help={parameter.help?.header_id} />
          {!isLinked ? (
            <LinkOutlined onClick={linkField} style={{ color: blue.primary }} />
          ) : (
            <div>
              <CloseSquareOutlined
                onClick={unlinkField}
                style={{ color: yellow[7] }}
              />
              <span className="ant-typography ant-typography-secondary" id="sharedFieldAlias" style={{ marginLeft: '5px', color: '#262626'}}>
                {sharedFieldAlias}
              </span>
            </div>
          )}
        </div>
      );

Declare the variable sharedFieldAlias outside the loop (just above the loop in your case).在循环外声明变量sharedFieldAlias (在你的例子中就在循环之上)。

let sharedFieldAlias;
<your loop goes here>

You have to declare the variable outside the loop.您必须在循环外声明变量。 It might also help to use a for loop instead of forEach so you can break out of the loop once the value is found.使用 for 循环而不是 forEach 也可能会有所帮助,这样您就可以在找到值后跳出循环。

 let sharedFieldAlias; for (const sharedField of sharedfields) { if (value.shared_field_uuid == sharedField.uuid) { sharedFieldAlias = sharedField.alias; console.log(sharedFieldAlias); break; } }

There's no need for a global variable.不需要全局变量。 Use find to check the array, find an object where the uuids match, and return the alias, otherwise return 'None'.使用find检查数组,找到 uuid 匹配的 object,并返回别名,否则返回 'None'。

 const sharedFields = [ { alias: 'Bob', uuid: '1' }, { alias: 'Carol', uuid: '2' }, { alias: 'Sam', uuid: '3' }, { alias: 'Moses', uuid: '4' } ]; function getAlias(fields, uuid) { const found = fields.find(field => { return uuid === field.uuid; }); return found? found.alias: 'None'; } console.log(getAlias(sharedFields, '3')); console.log(getAlias(sharedFields, '31')); console.log(getAlias(sharedFields, '2'));

Here's a small working example in React.这是 React 中的一个小的工作示例。

 function getAlias(fields, uuid) { const found = fields.find(field => { return uuid === field.uuid; }); return found? found.alias: 'None'; } function Example({ data }) { return ( <div> <div>{getAlias(data, '3')}</div> <div>{getAlias(data, '31')}</div> <div>{getAlias(data, '2')}</div> </div> ); } const data = [ { alias: 'Bob', uuid: '1' }, { alias: 'Carol', uuid: '2' }, { alias: 'Sam', uuid: '3' }, { alias: 'Moses', uuid: '4' } ]; ReactDOM.render( <Example data={data} />, document.getElementById('react') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="react"></div>

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

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