简体   繁体   English

如何根据它们的 OBJECTID 找出两个嵌套对象数组的差异?

[英]How to find the difference of two nested Object Arrays based on their OBJECTID?

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?我知道如何使用字符串/数字过滤数组,但是如何根据 OBJECTID 过滤嵌套的 Object 数组? (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.我尝试做一个 for 循环,并检查 OBJECTIDs,但页面由于无限循环而崩溃。 Please could anyone help?请问有人可以帮忙吗?

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. filter 方法将调用您的内部函数并将数组的每个元素作为参数传递。

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)

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)

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));

1) Gather ids into test_ids array which need to filter 1) 将ids收集到需要过滤的 test_ids 数组中
2) Use filter on geojson array and check if not included in above array. 2) 在 geojson 数组上使用过滤器并检查是否未包含在上述数组中。

 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);

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

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