D
D
Dmitry Kuznetsov2020-07-08 21:40:41
JavaScript
Dmitry Kuznetsov, 2020-07-08 21:40:41

How can a parsed JSON Object compare against certain fields in another JSON object?

All the good time of the day.

Tell me where to dig, otherwise I will not understand.

Task:
There are 2 XML files with approximately the same content, only the parameters are different, perhaps there are no properties. You need to compare these types by names, lifetime, etc. (the list is your own, customizable). and write it down in a document.

Examples:

1st file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<types>
    <type name="ACOGOptic">
        <nominal>15</nominal>
        <lifetime>7200</lifetime>
        <restock>1800</restock>
        <min>8</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
        <category name="weapons"/>
        <usage name="Military"/>
    </type>
    <type name="AK101">
        <nominal>5</nominal>
        <lifetime>10800</lifetime>
        <restock>1800</restock>
        <min>2</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="1" count_in_hoarder="1" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
        <category name="weapons"/>
        <usage name="Military"/>
        <value name="Tier4"/>
    </type>
    <type name="AK101_Black">
        <nominal>0</nominal>
        <lifetime>10800</lifetime>
        <restock>0</restock>
        <min>0</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="1" deloot="0"/>
        <category name="weapons"/>
    </type>



2nd file

<type name="ACOGOptic">
        <nominal>15</nominal>
        <lifetime>17200</lifetime>
        <restock>11800</restock>
        <min>3</min>
        <quantmin>-1</quantmin>
        <quantmax>-2</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
        <category name="weapons"/>
        <usage name="Military"/>
    </type>
    <type name="AK101">
        <nominal>5</nominal>
        <lifetime>10800</lifetime>
        <restock>1800</restock>
        <min>2</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="1" count_in_hoarder="1" count_in_map="0" count_in_player="0" crafted="0" deloot="0"/>
        <category name="weapons"/>
        <usage name="Military"/>
        <value name="Tier4"/>
    </type>
    <type name="AK101_Black">
        <nominal>0</nominal>
        <lifetime>10600</lifetime>
        <restock>0</restock>
        <min>1</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="1" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="1" deloot="0"/>
        <category name="weapons"/>
    </type>
    <type name="AK101_Green">
        <nominal>0</nominal>
        <lifetime>12800</lifetime>
        <restock>0</restock>
        <min>0</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="1" deloot="0"/>
        <category name="weapons"/>
    </type>



I was able to parse from XML to JSON. I was able to determine which names are and which are not. But I still can't understand how to compare in other parameters.

Actually code:
static comareFiles(newFile, oldFile) {
    // Первый файл
    var newFileCompare = this.compareFile(newFile);
    var newFileCompareParse = JSON.parse(newFileCompare);
    var newFileCompareParse = newFileCompareParse.types.type;

    // Второй файл
    var oldFileCompare = this.compareFile(oldFile);
    var oldFileCompareParse = JSON.parse(oldFileCompare);
    var oldFileCompareParse = oldFileCompareParse.types.type;

    newFileCompareParse.forEach((element, count) => {
      let searchName = element.attributes.name;

      // Сравнение по именам
      let nameSearching = oldFileCompareParse.find(
        (oldFile) => oldFile.attributes.name === searchName
      );

      if (!nameSearching) {
        console.log("Не найдено: " + element.attributes.name);
      }
    });
  }


JSON example after parsing:
[
  {
    attributes: { name: 'AKM' },
    lifetime: { text: 10800 },
    restock: { text: 1800 },
    quantmin: { text: -1 },
    quantmax: { text: -1 },
    cost: { text: 100 },
    flags: { attributes: [Object] },
    category: { attributes: [Object] }
  },
  {
    attributes: { name: 'AKS74U' },
    lifetime: { text: 10800 },
    restock: { text: 1800 },
    quantmin: { text: -1 },
    quantmax: { text: -1 },
    cost: { text: 100 },
    flags: { attributes: [Object] },
    category: { attributes: [Object] }
  }
]


Please provide an example algorithm. Thanks in advance!)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
McBernar, 2020-07-08
@McBernar

You cannot write to a js file (unless you are working in a node), but you can compare in a loop using hasOwnProperty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question