在数组位置移动值时没有得到正确的输出

Not getting proper output when shifting values in array position

我有 1 个数组,其中我的源和目标是这样的:

markers.push({
        "Location": "Chicago",
        "IsLocation": "Yes"
            });

    markers.push({
        "Location": "Los Angeles",
        "IsLocation": "Yes"
            });

Now when i will create points with my dynamic textbox then i would like to add those all points in between source and destination.

场景 1:第一个 动态文本框输入说 Eg:abc

markers[0]:Chicago
markers[1]:abc
marker[2]:Los Angeles.

场景 2:2nd 动态文本框输入说 Eg:pqr

markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
marker[3]:Los Angeles.

场景 3:3rd 带有输入的动态文本框说 Eg:lmn

markers[0]:Chicago
markers[1]:abc
markers[2]:pqr
markers[3]:lmn
marker[4]:Los Angeles.

我的第一个位置将被固定。

代码:

// Code goes here

var cnt = 1;
var maxNumberOfTextboxAllowed = 5;

var autocomplete = [];
var markers = [];

markers.push({
  "Location": "Chicago",
  "IsLocation": "Yes"
});

markers.push({
  "Location": "Los Angeles",
  "IsLocation": "Yes"
});

function Generatetextbox() {
  if (cnt <= maxNumberOfTextboxAllowed) {
    var fieldWrapper = $("<div class='fieldwrapper' id='field" + cnt + "'/>");
    var fName = $("<input type='text' class='fieldname' id='Txtopt" + cnt + "'  name='TxtoptNm" + cnt + "'  />");
    fieldWrapper.append(fName);
    fieldWrapper.append('<br />');
    fieldWrapper.append('<br />');
    $("#abc").append(fieldWrapper);
    var newInput = [];
    var newEl = document.getElementById('Txtopt' + cnt);
    var txtboxId = 'Txtopt' + cnt;
    newInput.push(newEl);
    setupAutocomplete(autocomplete, newInput, 0, txtboxId);
    cnt = cnt + 1;
  } else
    alert("Cant create more than 5 textbox")
}


function setupAutocomplete(autocomplete, inputs, i, txtboxId) {
  autocomplete.push((txtboxId));
  var idx = autocomplete.length - 1;
  document.getElementById(autocomplete[idx]).addEventListener("change", function() {
    alert(document.getElementById(autocomplete[idx]).value);
    var autoTextbox = [{
      "Location": document.getElementById(autocomplete[idx]).value,
      "IsLocation": "Yes"
    }]

    var markerLastIndexData = [{
      "Location": markers[markers.length - 1].Location,
      "IsLocation": "Yes"
    }]

    markers[markers.length - 1] = autoTextbox;
    markers[markers.length] = markerLastIndexData;
    console.log(markers)
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="abc"></div>
<button onclick="Generatetextbox()" class="btn btn-primary" type="button">Add</button>

您可以签入 console.I 我得到的结果不正确。

得到这样的输出:

控制台输出未定义:

预期输出显示在我的场景中,例如:

marker[0]:{ 
          Location="Chicago",  
          Isolcation="Yes"
          }
marker[1]:{ 
          Location="abc",  
          Isolcation="Yes"
          }
etc......

发生这种情况是因为在 setupAutocomplete 函数中,您将数组而不是对象分配给标记数组。只需删除声明将要推送到标记数组的两个变量的行上的 []。