如何使用javascript中的子字符串值查找数组的索引并替换为新字符串?

时间:2019-02-04 11:47:52

标签: javascript

我要弦-

["1-2","10-4","2-3","3-1","4-4","5-2","6-4","7-3","8-1","9-2"]

作为GetOption函数的输出,其中事件-QuestionID和event1 -OptionID

项目在线考试系统

var getValue;
var getName = new Array();
var temp = new Array();

function GetOption(event, event1) {
   debugger;

   if (temp.includes(event)) {
      var x = getName.indexOf(event);
      getName.splice(x - 1, 1);
      getName.includes(event);
   }

   this.event1 = event1;
   temp.push(event);

   var getValue = event + "-" + event1;

   if (getValue == "undefined-undefined") {
      getName.push("");
   } else {
      getName.push(getValue);
      getName.sort();
      alert(getName);
   }

   $("#resultHidden").val(getName);
}

1 个答案:

答案 0 :(得分:0)

var items = ["1-2","10-4","2-3","3-1","4-4","5-2","6-4","7-3","8-1","9-2"];
var searchItems = "1-2";
var newItems = "2-1";
for (var i = 0; i < items.length; i++) {
    if (items[i].startsWith(searchItems)) {
        items[i] = newItems;
    }
}