【问题标题】:js function that created a specific object taking values from SerializeArray创建从 SerializeArray 中获取值的特定对象的 Node.js 函数
【发布时间】:2020-03-06 09:15:48
【问题描述】:

我有这个从serializeArray() 函数获取的数组,我想将该键/值传递给特定的 js 对象。

我有来自$("selector :pseudoSelector").serializeArray()的这个结果

然后给我这个:

[
  {
    "name": "Address",
    "value": "Calle pizzarro 56"
  },
  {
    "name": "1",
    "value": "83"
  },
  {
    "name": "3",
    "value": "340"
  },
  {
    "name": "5",
    "value": "340"
  },
  {
    "name": "7",
    "value": "hi how are you?"
  },
  {
    "name": "latitud",
    "value": "87798"
  },
  {
    "name": "longitude",
    "value": "-8,208914"
  }
]

然后我想将这些数据转换为这样的对象:

{
  "id": 0, //this id is the last id on this list plus 1(so first is 0 next one 1 and so on..) 
  "address": "Calle pizzarro 56",
  "appliesOn": 1, // also default value so dont worry
  "areas": [
    {
      "id": 83,
      "areaLevelId": 1,
      "name": "Portugal",
      "areaPosition": null,
      "parentAreas": [],
      "childAreas": []
    },
    {
      "id": 340,
      "areaLevelId": 3,
      "name": "Algarve",
      "areaPosition": null,
      "parentAreas": [],
      "childAreas": []
    },
    {
      "id": 340,
      "areaLevelId": 5,
      "name": "Albufeira",
      "areaPosition": null,
      "parentAreas": [],
      "childAreas": []
    },
    {
      "id": 12485,
      "areaLevelId": 7,
      "name": "hi how are you?",
      "areaPosition": {
        "latitude": 234,
        "longitude":79879
      },
      "parentAreas": [],
      "childAreas": []
    }
  ],
  "recordStatus": 1 //it handles by other way so for now 1 is ok
}

问题在于区域,当 name == 1 时,值变为 areaLevelId = 1,依此类推。当areaLevelId=7 出现异常并获取latitude 和longitude 并使用它创建一个数组。 到目前为止我所做的是:

function objectifyForm(fields) {

        var returnArray = {};

        returnArray["areas"] = [];

        jQuery.each(fields, function (i, field) {

            if (!isNaN(field.name) && field.value != "") {

                returnArray["areas"].push({
                    "id": (field.value.toLowerCase()),
                    "areaLevelId": (field.name)
                })

            } else if (field.name == "latitud" && field.name == "longitude") {
                //returnArray["areas"][3].push({
                //    returnArray["areas"]["areaPosition"] = [];
                //})
            } else {
                returnArray[field.name.toLowerCase()] = field.value + "";
            }
        });
        return returnArray;
    }

但这是错误的,因为我不知道如何继续。 这是我第一次做类似的事情;

1) 从带有serializeArray()的表单中获取值

2) 然后创建一个函数,将该信息放入具有具体形状的对象中。

3) 将该数组推送到全局数组

pd: parentAreas, childAreas 总是 empty ,并且 areaPosition 在areaLevelId == 7 时有latitude 和longitude 我怎么能做到这一点??非常感谢你。

【问题讨论】:

    标签: javascript jquery html arrays json


    【解决方案1】:

    您可以执行以下操作:

    var arr = [
      { name: "Address", value: "Calle pizzarro 56" },
      { name: "1", value: "83" },
      { name: "3", value: "340" },
      { name: "5", value: "340" },
      { name: "7", value: "hi how are you?" },
      { name: "latitud", value: "87798" },
      { name: "longitude", value: "-8,208914" }
    ];
    
    function objectifyForm(fields) {
      var address = fields.find(function(item) {
        return item.name === "Address";
      });
      var latitude = fields.find(function(item) {
        return item.name === "latitud";
      });
      var longitude = fields.find(function(item) {
        return item.name === "longitude";
      });
      var areas = fields.filter(function(item) {
        return (
          item.name !== "Address" &&
          item.name !== "latitud" &&
          item.name !== "longitude"
        );
      });
    
      var result = {
        id: 0,
        address: address.value,
        appliesOn: 1,
        recordStatus: 1
      };
    
      result.areas = areas.map(function(area) {
        return {
          id: area.value,
          areaLevelId: area.name,
          name: "Whatever",
          areaPosition:
            area.name === "7"
              ? { latitude: latitude.value, logitude: longitude.value }
              : null,
          parentAreas: [],
          childAreas: []
        };
      });
    
      return result;
    }
    
    var result = objectifyForm(arr);
    
    console.log(result);

    我在填写areas数组之前提取了相关信息,稍微简化了objectifyForm函数,所以它更容易使用。

    另外,我不确定你从哪里得到areas[index].name,但我会留给你。

    希望这会有所帮助。

    【讨论】:

    • 这正是我所需要的!!,非常感谢我冷静地阅读以了解原因并适用于其他表格。areas[index].name 是自动的,如果它有它的AreaLevel 和正确的id。所以非常感谢!
    猜你喜欢
    • 2018-11-05
    • 1970-01-01
    • 2019-10-08
    • 2017-11-22
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多