【问题标题】:Javascript How do i edit and update an objectJavascript 如何编辑和更新对象
【发布时间】:2020-02-22 14:44:57
【问题描述】:

JavaScript 新手!如果我想编辑和更新具有预先存在值的对象怎么办?

array [];

// this function is for pre-existed value
function object1 ( mytext ) {
this.text = mytext
}

var usertext = new object ("my text to edit");
memArray.push (usertext);
console.log (usertext)

如何编辑/更新提示值?

function object2(){
   var text
   this.getData = function(){
       return{
       text:  this.text
}
}
   this.setData = function (){
       this.text = prompt ("Type anything");
}
   return this.setData();
}
   var user = new addUserObject;
       memArray.push(user.getData());
       console.log(memArray);

【问题讨论】:

    标签: arrays object prompt


    【解决方案1】:

    有很多方法,但我的方法是使用“for”迭代到您要编辑的单词并更改它。

    var myArray = ["hello" , "hi"];
    console.log(myArray);
    
    function addToArray(str){
      myArray.push(str);
    }
    addToArray("yo");
    console.log(myArray);
    
    function editArray(){
      var word = prompt("Please enter which word you want to change:", "");
      if (word != null && word.length > 0){
        var found = false;
        for (var i = 0; i < myArray.length; i++){
          if (word == myArray[i]){
            found = true;
            var editWord = prompt("You want to change \"" + word + "\" to:", "");
            if (editWord != null && editWord.length > 0){
              myArray[i] = editWord;
            } else {
              console.log("You input nothing.");
            }
            break;
          }
        }
        if (!found){
          console.log("Cannot find this word");
        }
      } else {
        console.log("You input nothing.");
      }
    
    }
    editArray();
    console.log(myArray);

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 1970-01-01
      • 2011-06-08
      • 2014-11-27
      • 1970-01-01
      • 2015-01-18
      • 2021-07-19
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多