1

I am struggling to find an answer to this code. I know how to filter arrays with strings/numbers, but how do I filter a nested Object array based on the OBJECTID? (Apologies, this is a more relevant example to what I am doing)

let geojson = [
  {properties: {OBJECTID: 6249646, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}}, 
  {properties: {OBJECTID: 6249646, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}}, 
  {properties: {OBJECTID: 6249647, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249647, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249648, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249649, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}} 
  ]


let newjson = [
  {properties: {OBJECTID: 6249647, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249648, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249649, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249650, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
  {properties: {OBJECTID: 6249651, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}}
  ]

What I want: [
    {properties: {OBJECTID: 6249650, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}},
    {properties: {OBJECTID: 6249651, FeatureCode: 10185, Version: 3, VersionDate: "1/25/2018"}}
  ]

This is just a smaller 'sample' of the code I actually have. I tried doing a for loop, and checking the OBJECTIDs, but the page just crashed due to infinite loops. Please could anyone help?

jlsr10
  • 33
  • 5
  • Does this answer your question? [How to get the difference between two arrays of objects in JavaScript](https://stackoverflow.com/questions/21987909/how-to-get-the-difference-between-two-arrays-of-objects-in-javascript) – Heretic Monkey Feb 28 '20 at 15:08

4 Answers4

0
geojson.filter( obj => obj.properties.ID == 1 || obj.properties.ID == 2)

Why not using the filter?

Explanation: You can just return a true or a false to tell the filter method if is matches your conditions or not. the filter method will call your inner function and pass each element of the array as argument.

If you have an larger array you can do something like this:

fil=[1,2,4];
geojson.filter( obj => fil.indexOf(obj.properties.ID) !== -1)
KingKabyle
  • 381
  • 1
  • 6
0

Try the below code, maybe it gives you an idea. But I am not sure why it is not working as expected

let geojson = [{properties: {ID: 1}, name: "Jimmy"}, 
               {properties: {ID: 2}, name: "Joe"}, 
               {properties: {ID: 3}, name: "Ji"}, 
               {properties: {ID: 4}, name: "my"}]


let test = [{properties: {ID: 3}, name: "Ji"}, {properties: {ID: 4}, name: "my"}]

var mappedTestArray = test.map(testObject => {
    return testObject.properties
})

console.log(mappedTestArray)

let geoJson = geojson.filter(geoObject => {
    console.log(geoObject.properties)
    console.log(mappedTestArray.includes(geoObject.properties))
   return mappedTestArray.some(el => el === geoObject.properties)
})

console.log(geoJson)
0

Here is the simple solution if you need to find objects in the array by multiple values

const neededIDs = [1,2,3, ...];

const neededObjects = geojson.filter(item => neededIDs.includes(item.properties.ID));

Maksym Bezruchko
  • 1,223
  • 6
  • 23
0

1) Gather ids into test_ids array which need to filter
2) Use filter on geojson array and check if not included in above array.

let geojson = [
  { properties: { ID: 1 }, name: "Jimmy" },
  { properties: { ID: 2 }, name: "Joe" },
  { properties: { ID: 3 }, name: "Ji" },
  { properties: { ID: 4 }, name: "my" }
];

let test = [
  { properties: { ID: 3 }, name: "Ji" },
  { properties: { ID: 4 }, name: "my" }
];

const test_ids = test.map(x => x.properties.ID);

const res = geojson.filter(gj => !test_ids.includes(gj.properties.ID));

console.log(res);
Siva K V
  • 10,561
  • 2
  • 16
  • 29