benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.78k stars 466 forks source link

How to use `propertyFilter`? #313

Open cgraham-rs opened 2 years ago

cgraham-rs commented 2 years ago

I'm looking to exclude specific keys from nested objects being included in the diff but am struggling to understand how to successfully implement propertyFilter to accomplish this. Could someone provide a clear code example?

From the example data below I'd like to exclude: surface demographics.population countries[].independence

Example data taken from live demo:

{
  "name": "South America",
  "summary": "South America (Spanish: América del Sur, Sudamérica or  \nSuramérica; Portuguese: América do Sul; Quechua and Aymara:  \nUrin Awya Yala; Guarani: Ñembyamérika; Dutch: Zuid-Amerika;  \nFrench: Amérique du Sud) is a continent situated in the  \nWestern Hemisphere, mostly in the Southern Hemisphere, with  \na relatively small portion in the Northern Hemisphere.  \nThe continent is also considered a subcontinent of the  \nAmericas.[2][3] It is bordered on the west by the Pacific  \nOcean and on the north and east by the Atlantic Ocean;  \nNorth America and the Caribbean Sea lie to the northwest.  \nIt includes twelve countries: Argentina, Bolivia, Brazil,  \nChile, Colombia, Ecuador, Guyana, Paraguay, Peru, Suriname,  \nUruguay, and Venezuela. The South American nations that  \nborder the Caribbean Sea—including Colombia, Venezuela,  \nGuyana, Suriname, as well as French Guiana, which is an  \noverseas region of France—are also known as Caribbean South  \nAmerica. South America has an area of 17,840,000 square  \nkilometers (6,890,000 sq mi). Its population as of 2005  \nhas been estimated at more than 371,090,000. South America  \nranks fourth in area (after Asia, Africa, and North America)  \nand fifth in population (after Asia, Africa, Europe, and  \nNorth America). The word America was coined in 1507 by  \ncartographers Martin Waldseemüller and Matthias Ringmann,  \nafter Amerigo Vespucci, who was the first European to  \nsuggest that the lands newly discovered by Europeans were  \nnot India, but a New World unknown to Europeans.",
  "surface": 17840000,
  "timezone": [
    -4,
    -2
  ],
  "demographics": {
    "population": 385742554,
    "largestCities": [
      "São Paulo",
      "Buenos Aires",
      "Rio de Janeiro",
      "Lima",
      "Bogotá"
    ]
  },
  "languages": [
    "spanish",
    "portuguese",
    "english",
    "dutch",
    "french",
    "quechua",
    "guaraní",
    "aimara",
    "mapudungun"
  ],
  "countries": [
    {
      "name": "Argentina",
      "capital": "Buenos Aires",
      "independence": "1816-07-09T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Bolivia",
      "capital": "La Paz",
      "independence": "1825-08-06T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Brazil",
      "capital": "Brasilia",
      "independence": "1822-09-07T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Chile",
      "capital": "Santiago",
      "independence": "1818-02-12T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Colombia",
      "capital": "Bogotá",
      "independence": "1810-07-20T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Ecuador",
      "capital": "Quito",
      "independence": "1809-08-10T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Guyana",
      "capital": "Georgetown",
      "independence": "1966-05-26T04:00:00.000Z",
      "unasur": true
    },
    {
      "name": "Paraguay",
      "capital": "Asunción",
      "independence": "1811-05-14T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Peru",
      "capital": "Lima",
      "independence": "1821-07-28T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Suriname",
      "capital": "Paramaribo",
      "independence": "1975-11-25T05:00:00.000Z",
      "unasur": true
    },
    {
      "name": "Uruguay",
      "capital": "Montevideo",
      "independence": "1825-08-25T04:56:02.000Z",
      "unasur": true
    },
    {
      "name": "Venezuela",
      "capital": "Caracas",
      "independence": "1811-07-05T04:56:02.000Z",
      "unasur": true
    }
  ]
}
prasanmgc commented 2 years ago

@benjamine Could you please provide an example on how to use propertyFilter? Your help much appreciated.

prasanmgc commented 2 years ago

@cgraham-rs try this. it worked. https://github.com/benjamine/jsondiffpatch/issues/258#issuecomment-1044456667