【问题标题】:JavaScript map(function) return this.value - finding the last element in the arrayJavaScript map(function) return this.value - 查找数组中的最后一个元素
【发布时间】:2021-09-25 01:38:21
【问题描述】:

按照上一个查询:

Javascript - map(function) return this.value - comma on wrong side

我有我想要区分的元素之一。 也就是说,我想要这个对象后面的“ - ”元素。

我在这里找到了一些解决方案:

Javascript Map Array Last Item

并试图摆弄我的代码,但不幸的是没有用。

HTML:

      <fieldset id="ophealth_safety">
      <div>
      <input type="checkbox" id="opladdert" name="health_safety" value="Triple ladders">
      <label class="checking" for="opladder">Triple Ladders</label>
      </div>
      <div>
      <input type="checkbox" id="opgardens" name="health_safety" value="Access private gardens required">
      <label class="checking" for="opgardens">Access private gardens required</label>
      </div>
      <div>
      <input type="checkbox" id="opskylight" name="health_safety" value="Skylight">
      <label class="checking" for="opskylight">Skylight</label>
      </div>
      <div>
      <input type="checkbox" id="oploft" name="health_safety" value="Access Loft">
      <label class="checking" for="oploft">Access Loft</label>
      </div>
      <div>
      <input type="checkbox" id="oproof" name="health_safety" value="Access to roof">
      <label class="checking" for="oproof">Access to roof</label>
      </div>
      <div>
      <input type="checkbox" id="opother" name="health_safety" value="Other">
                                                <label class="checking" for="opother">Other</label>
                                                <input type="text" id="opotherdesc" name="other" pattern="[A-Za-z].{4,}" title="The text should include at least 4 letters" placeholder="Please specify">
                                            </div>
                                        </fieldset>

JAVASCRIPT:

    var healthSafety = $('input:checkbox[name=health_safety]:checked').map(function() {
    const lastIndex = row.length - 1;
    row.map((rank, i) => {
        if (i === lastIndex) {
            .join(" - ")
        } else {
            return this.value;
            }).get().join(", ")
    });

我目前有:

健康和安全:需要进入私人花园、进入阁楼、其他天花板

我想要这样的东西:

健康和安全:需要进入私人花园,进入阁楼,其他 - 天花板

我的代码中缺少什么?

【问题讨论】:

  • 请重新格式化您的 html 代码,以便代码具有更好的可读性。谢谢。
  • 如果.map() 的回调没有在每个路径中都返回一个值,或者如果您不使用.map() 的返回值,那么.map() 不是正确的工具。
  • 为什么要嵌套 maps()?很难理解发生了什么?
  • row.slice(0, -1).join(", ") + " - " + row.slice(-1) 或只是一个普通的旧for 循环,或.reduce(),或...
  • 我已经格式化了 HTML 代码

标签: javascript html


【解决方案1】:

我不确定我是否能解决您的问题,但我会尽力提供帮助。

您要解决的第一个问题是获取一个以逗号分隔的复选框值列表,可能是这样的:

const selector = 'input[name="health_safety"]:checked';
const values = Object.values(document.querySelectorAll(selector)).map(input => input.value);

现在您已经有了列表,您想检查列表中是否有“其他”(最后一个元素)。如果是,您想在文本输入的值后面附加“-”:

let healthSafety = values.join(', ') + (
    values[values.length - 1] === 'Other' 
        ? ' - ' + document.getElementById('opotherdesc').value 
        : ''
);

https://jsfiddle.net/afe8otzn/3/

【讨论】:

  • 它适用于 JSFiddle,但如何将它与 var healthSafety = $('input:checkbox[name=health_safety]:checked') 我试过: var healthSafety = const selector = 'input[name ="health_safety"]:检查;'我认为一切都应该在某个功能范围内
  • 我不确定我理解你的意思。结果字符串存储在result 中。如果您愿意,您可以将其分配给healthSafety :)
  • 你能告诉我如何将它包含在 var healthSafety 中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2022-01-21
  • 1970-01-01
  • 2012-11-03
  • 2010-12-12
相关资源
最近更新 更多