在JSON数组中搜索特定的单词

时间:2019-02-01 12:42:37

标签: javascript json

尝试从用户输入的生成的JSON数组中查找单词“伦敦”。

响应200(API示例):

{
"latitude": 52.24593734741211,
"longitude": -0.891636312007904,
"addresses":
["10 Watkin Terrace, , , , , Northampton, Northamptonshire",
"12 Watkin Terrace, , , , , Northampton, Northamptonshire",

我写了一个函数:

 request.onload = function() {

var data = JSON.parse(this.response);

if(request.status >= 200 && request.status < 400) {
  var city = data.addresses;
  var london = 'London';
   var check = city.includes(london);
    if(check) {
      console.log(city);
    } else {
      console.log('not in london');
       var show = document.getElementById('incorrect_postcode').style.visibility='visible'; 

我不确定我是否误解了API还是这里出了点问题...该语句始终为假。

也试过for循环搜索数组的长度来定位“伦敦”,没有效果

谢谢。

4 个答案:

答案 0 :(得分:2)

addresses数组是一个字符串列表,每个字符串都是一个地址。因此,所有字符串都不完全等于'London',因为地址比城市多。

您可以遍历地址数组,并检查每个字符串是否包含'London',但这也将匹配街道名称中伦敦所在的任何地址。

更好的方法是遍历地址数组并解析每个地址字符串以提取城市值。然后将城市值与'London'进行比较。

注意:我假设倒数第二个城市是。

const data = {
  "latitude": 52.24593734741211,
  "longitude": -0.891636312007904,
  "addresses": [
    "10 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "12 Watkin Terrace, , , , , Northampton, Northamptonshire",
    "221B Baker Street, , , , , London, "
  ]
};



let desiredCity = 'London';
// array index of city after splitting the addresses
const CITY_POSITION = 5;

console.log('Addresses in ' + desiredCity + ':');

for(let i = 0; i < data.addresses.length; i++) {
  // split the address on the delimiter, ', '
  let addr = data.addresses[i].split(', ');
  
  if(addr[CITY_POSITION] === desiredCity) {
    console.log(data.addresses[i]);
  }
}

答案 1 :(得分:0)

欢迎来到

如果我们假设JSON请求正在运行,则无需包含该部分。

您的示例不起作用的原因是,其中包括寻找完整的字符串

相反,遍历数组并查看每个字符串

注意:如果您使用search或indexOf,则可能会在Londonderry找到伦敦

var response = `{
"latitude": 52.24593734741211,
"longitude": -0.891636312007904,
"addresses":
[
"10 Watkin Terrace, , , , , Northampton, Northamptonshire, NN1 1ED",
"Bishop Street,     , , , , Londonderry, BT48 6PQ",
"12 Watkin Terrace, , , , , Northampton, Northamptonshire, NN1 1ED",
"10 Downing Street, , , , , LONDON, SW1A 2AA"
]
}`

var data = JSON.parse(response);

var addrs = data.addresses;
console.log(addrs)
var city = 'London'.toLowerCase();
var check = addrs.filter(function(addr) {
  var thisCity = addr.split(",")[5].toLowerCase().trim()
  console.log(city,thisCity)
  return  thisCity === city
})
if (check.length > 0) {
  console.log("Yes, in "+city,check);
} else {
  console.log('not in london');
}

答案 2 :(得分:0)

如果您只想检查是非,则可以像这样

var input = {
  "latitude": 52.24593734741211,
  "longitude": -0.891636312007904,
  "addresses":[
    "10 Watkin Terrace, Northampton, Northamptonshire",
    "12 Watkin Terrace, Northampton, Northamptonshire",
    //"Baker Street, London, England",
    "Londonderry, Ireland"
  ]
};

const isLondon = obj => obj.addresses.some(addr => addr.match(/, London,/));

console.log(isLondon(input));

答案 3 :(得分:0)

这是一个简单的搜索

var data ={
"latitude": 52.24593734741211,
"longitude": -0.891636312007904,
"addresses":
["10 Watkin Terrace, , , , , Northampton, Northamptonshire",
"12 Watkin Terrace, , , , , Northampton, Northamptonshire",
 "london", 
 "londonderry, Ireland",
 "London Road"
]}

var searchFor = "london";
console.log(data.addresses.filter((item) => {
 return item.toLowerCase().split(" ").indexOf(searchFor.toLowerCase()) != -1
}))