简体   繁体   English

获取 javascript 数组第一个元素

[英]Get javascript array first element

How can I get the first element of an array in javascript by a means other than array[0] ?如何通过array[0]以外的方式获取 javascript 中数组的第一个元素?

I do not know why, but for some reason the array that gets passed into the method has its first, and only, element at index 5, not 0.我不知道为什么,但由于某种原因,传入该方法的数组的第一个也是唯一一个元素位于索引 5,而不是 0。

Edit - Here is my attempt at providing a minimal reproducible example.编辑- 这是我尝试提供一个最小的可重现示例。

      // Initialize database connection string datatable editor
  editor = new $.fn.dataTable.Editor({
    table: "#remoteSqlDatabaseTable",
    ajax: $("#UpdateIntegrationRemoteSqlDatabaseAction").val(),
    fields: [
      { label: 'Reference Name', name: 'Name' },
      { label: 'IP Address or Host', name: 'IpAddress' },
      { label: 'Port', name: 'Port' },
      { label: 'Username', name: 'UserCredential' },
      { label: 'Password', name: 'PasswordCredential', def: '' },
      { name: 'button' }
    ]
  });

  editor
    .on('open', function (e, type) {
      if (type === 'inline') {
        // Listen for a tab key event when inline editing
        $(document).on('keydown.editor', function (e) {
          if (e.keyCode === 9 || e.keyCode === 13) {
            editor.blur();
          }
        });
      }
      if (editor.s.includeFields[0] === 'PasswordCredential') {
        editor.field(editor.s.includeFields[0]).val('');
      }
    })
    .on('close', function () {
      $(document).off('keydown.editor');
    })
    .on('preSubmit', function (e, edit) {
      if (edit.data[1].PasswordCredential !== undefined) {
        var value = edit.data[1].PasswordCredential;
        // check for null or whitespace
        if (typeof value === 'undefined' || value == null || value.replace(/\s/g, '').length < 1) {
          editor.close();
          return false;
        }
      }
    });


  // Configure inline editing columns
  $('#remoteSqlDatabaseTable').on('click', 'tbody td:not(:last-child)', function (e) {
    editor.inline(this, {
      submitOnBlur: true
    });
  });

  // Reload data on edit to handle reordering
  //editor.on("submitSuccess", function () {
  //  var table = $('#databaseConnectionTable').DataTable();
  //  console.log('submitSuccess');
  //  table.ajax.reload(function () {
  //    initTableStyling();
  //  });
  //})

  // Initialize datatable
  var table = $('#remoteSqlDatabaseTable').DataTable({
    paging: false,
    searching: false,
    info: false,
    ordering: false,
    ajax: $("#GetIntegrationRemoteSqlDatabasesAction").val(),
    dom: 'Bfrtip',
    columns: [
      { data: 'Name' },
      { data: 'IpAddress' },
      { data: 'Port' },
      { data: 'UserCredential' },
      { data: 'PasswordCredential' },
      { data: null }
    ],
    select: false,
    responsive: false,
    buttons: [],
    columnDefs: [
      {
        targets: -1,
        data: null,
        render: function (data, type, row, meta) {
          return '<button class="btn red" type="button">Delete</button>';
        }
      }
    ],
    initComplete: function (settings, json) {
      //initTableStyling();
    }
  });

Like i said, i am not sure exactly where the problem is coming from.就像我说的,我不确定问题出在哪里。 The method that is breaking is the editor.on('presubmit'), where it check if(edit.data[1]. edit.data[1] is the object that only has a 5th element.破坏的方法是 editor.on('presubmit'),它检查 if(edit.data[1].edit.data[1] 是只有第 5 个元素的 object。

this is everything relating to the table whos editing is the problem.这是与谁的编辑是问题的表有关的一切。

You can do this:你可以这样做:

let [first_element] = your_array

Updated answer, this works with objects.更新的答案,这适用于对象。 It isn't a pretty as find filter answers but it simply works.它不像find filter答案那样漂亮,但它很有效。

Loop through the object and set a value.循环通过 object 并设置一个值。 Since there is only one element, it will set the variable without overwriting anything.由于只有一个元素,它将设置变量而不覆盖任何内容。

 testArray = {"5": "test"}; var val = ""; for(key in testArray){val = testArray[key];} console.log(val);

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

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