2

I have the following 2 arrays being returned from a function I'm wanting to go through Array A and if one of those values is found in Array B, i want to save the coordinates. At the moment what i've tried returns nothing. (I'm just trying to return if anotherArray includes one of the collabs

Array A

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

Filter Array

    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
        ]
    }
]

What i've tried

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

Expected Output

res = [

 matchingObject: {
     "collabs": "University of Wales, Bangor",
        "coordinates": [
            -4.13,
            53.23
        ]
}
]
3
  • expected output ? Commented Mar 16, 2019 at 13:13
  • array B is not valid Javascript or JSON. Commented Mar 16, 2019 at 13:14
  • Updated OP with a valid JSON and expected output Commented Mar 16, 2019 at 13:21

3 Answers 3

2

Your filterArray is not a valid array. You should pass each item as an object like this:

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
]

Your approach of filtering each element is good, it's just that you have to compare f.collabs, as f represents each object in the array.

res = filterArray.filter(f => anotherArray.includes(f.collabs);
Sign up to request clarification or add additional context in comments.

Comments

1

Like Dan mentioned your JSON isnt valid. Here's the corrected format and change your filter to this.

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

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

Comments

1

Use filter function to filter data and map function as projection

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.