繁体   English   中英

如何将一个数组与另一个数组匹配,但将匹配的元素保存在JavaScript中

[英]How to match one array to another array but saving matching elements in javascript

我从要通过数组A的函数中返回了以下2个数组,如果在数组B中找到了其中一个值,我想保存坐标。 目前,我尝试过的操作没有任何回报。 (我只是想返回是否anotherArray包括其中一个协作对象

阵列A

anotherArray = 
[
    "Jadavpur University",
    "University of Wales, Bangor",
    "University of Wales, Bangor",
    "University of Wales, Bangor",
    "University of Aberdeen",
    "University of Glasgow"
]

过滤阵列

    filter [
    {
        "collabs": "Agri-food,18a Newforge Lane, Belfast",
        "coordinates": [
            -5.943629,
            54.559632
        ]
    },
    {
        "collabs": "Manga Cl, Nairobi, Kenya",
        "coordinates": [
            36.81667,
            -1.28333
        ]
    },
    {
        "collabs": "University of Dundee",
        "coordinates": [
            -2.97,
            56.464
        ]
    },
    {
        "collabs": "University of Leeds",
        "coordinates": [
            -1.54917,
            53.79972
        ]
    },
    {
        "collabs": "Scottish Bowel Screening Centre",
        "coordinates": [
            -86.6396113,
            33.6535795
        ]
    },
    {
        "collabs": "NHS Greater Glasgow & Clyde",
        "coordinates": [
            -4.066085,
            55.797777
        ]
    },
    {
        "collabs": "University of Aberdeen",
        "coordinates": [
            -2.140831,
            57.118693
        ]
    },
    {
        "collabs": "CEH Edinburgh",
        "coordinates": [
            -73.660483,
            45.456296
        ]
    },
    {
        "collabs": "Anhui University",
        "coordinates": [
            -77.8624,
            40.8005
        ]
    },
    {
        "collabs": "University of Wales, Bangor",
        "coordinates": [
            -4.13,
            53.23
        ]
    },
    {
        "collabs": "Food and Environment Research Agency (FERA)",
        "coordinates": [
            1.55528,
            42.55833
        ]
    },
    {
        "collabs": "Centre for Environment, Fisheries and Aquaculture Science (CEFAS)",
        "coordinates": [
            1.55528,
            42.55833
        ]
    },
    {
        "collabs": "University of Edinburgh",
        "coordinates": [
            -3.19889,
            55.95
        ]
    },
    {
        "collabs": "University of Manchester",
        "coordinates": [
            -2.275228,
            53.488028
        ]
    },
    {
        "collabs": "University of Aberdeen",
        "coordinates": [
            -2.140831,
            57.118693
        ]
    },
    {
        "collabs": "University of Natural Resources and Life Sciences",
        "coordinates": [
            1.55528,
            42.55833
        ]
    },
    {
        "collabs": "Jadavpur University",
        "coordinates": [
            77.2146,
            28.6882
        ]
    },
    {
        "collabs": "Noakhali Science and Technology University (NSTU)",
        "coordinates": [
            25.205,
            59.071
        ]
    },
    {
        "collabs": "Plymouth Marine Laboratory",
        "coordinates": [
            -93.4665,
            45.0065
        ]
    },
    {
        "collabs": "Huazhong Agricultural University",
        "coordinates": [
            -123.25275,
            49.266565
        ]
    }
]

我尝试过的

 res = filterArray.filter(f=> anotherArray.includes(f)); 

预期产量

res = [

 matchingObject: {
     "collabs": "University of Wales, Bangor",
        "coordinates": [
            -4.13,
            53.23
        ]
}
]

您的 filterArray不是有效的数组。 您应该将每个项目作为这样的对象传递:

filterArray = [
  {
    collabs: "Agri-food,18a Newforge Lane, Belfast",
    coordinates:  [-5.943629, 54.559632]
  },
  {
    collabs: "Manga Cl, Nairobi, Kenya"
    coordinates:  [36.81667, -1.28333]
  } // and so on
]

您过滤每个元素的方法很好,只是您必须比较f.collabs,因为f表示数组中的每个对象。

res = filterArray.filter(f => anotherArray.includes(f.collabs);

像Dan提到的那样,您的JSON是无效的。 这是更正后的格式,请将您的过滤器更改为此。

res = filterArray.filter(f=> anotherArray.includes(f.collabs)); 
console.log(res)

https://jsfiddle.net/jfwcvLbn/1/

使用过滤器功能过滤数据并将地图功能用作投影

var filterdData = filterArray.filter(function(t){ return anotherArray.indexOf(t.collabs) !== -1 }).map(function(t){return t.coordinates;});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM