从JSON自动完成

时间:2017-03-06 14:56:44

标签: jquery json

我正在尝试使用http://easyautocomplete.com显示自动完整文本字段。

使用他们的演示效果很好。

他们的json就像这样显示

[ 
  {"name": "Afghanistan", "code": "AF"}, 
  {"name": "Aland Islands", "code": "AX"}, 
  {"name": "Albania", "code": "AL"}, 
  {"name": "Algeria", "code": "DZ"}, 
  {"name": "American Samoa", "code": "AS"}, 
  ...
 ]

我的代码显示在哪里

[
  {
    "event": {
      "id": "28139949",
      "name": "Comp (FRA) 6th Mar",
      "countryCode": "FR",
      "timezone": "CET",
      "venue": "Compiegne",
      "openDate": "2017-03-06T12:15:00.000Z"
    },
    "marketCount": 5
  },
  {
    "event": {
      "id": "28139948",
      "name": "Yarm (FC) 6th Mar",
      "countryCode": "GB",
      "timezone": "Europe\/London",
      "venue": "Yarmouth",
      "openDate": "2017-03-06T18:33:00.000Z"
    },
    "marketCount": 11
  },
  ...
]

以上工作原理的演示代码如下:

var options = {
    url: "resources/countries.json",

    getValue: "name",

    list: {
        match: {
            enabled: true
        }
    }
};

我知道问题是因为我的名字'字段在'事件'领域,但我不知道如何定位。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

通过地图调用运行您的数据对象:

const data = [
  {
    "event": {
      "id": "28139949",
      "name": "Comp (FRA) 6th Mar",
      "countryCode": "FR",
      "timezone": "CET",
      "venue": "Compiegne",
      "openDate": "2017-03-06T12:15:00.000Z"
    },
    "marketCount": 5
  },
  {
    "event": {
      "id": "28139948",
      "name": "Yarm (FC) 6th Mar",
      "countryCode": "GB",
      "timezone": "Europe\/London",
      "venue": "Yarmouth",
      "openDate": "2017-03-06T18:33:00.000Z"
    },
    "marketCount": 11
  }
];

const newData = data.map(curr => {
  return {
     name: curr.event.name,
     code: curr.event.countryCode
  };
});

console.log(newData);