操作JavaScript对象的最佳方法是什么?

时间:2017-04-27 04:04:09

标签: javascript jquery arrays object

我有一个JavaScript对象:

<!DOCTYPE html>
<html>

  <head>
  </head>

  <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" class="telnumber" id="tel" maxlength="14" pattern="\d{6}-\d{2}-\d{4}" placeholder="999999-99-9999"/><label for="tel">Invalid input <code>999999-99-9999</code></label>
    <input type="reset">

    <script>
        $(".telnumber").on("input", function(e) {       
          if (!this.validity.patternMismatch) {
             this.labels[0].innerHTML = "Valid input";
             this.disabled = !this.validity.patternMismatch;
             $(this).siblings("input[type=reset]").show().one("click", function() {
               e.target.disabled = false;
               e.target.value = "";
               e.target.labels[0].innerHTML = "Invalid input <code>999999-99-9999</code>";
               $(this).hide();
             })
          };        
        }).siblings("input[type=reset]").hide()
    </script>
  </body>

</html>

我希望对象看起来像这样:

["campaign_id","campaign_name","campaign_type","campaign_status"]

使用Javascript操作对象的最佳方法是什么?

2 个答案:

答案 0 :(得分:6)

让我们在Javascript

中使用Map

&#13;
&#13;
    var result = ["campaign_id","campaign_name","campaign_type","campaign_status"].map(a => (
          {
            title: a, 
            name: a
          }
    ));

    console.log(result);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以这样做:

var oldArray = ["campaign_id","campaign_name","campaign_type","campaign_status"]

var newArray = oldArray.map((item) => {
    return {"name": item, "title": item}
})