简体   繁体   English

如何使用TypeScript比较两个JSON文件的内容?

[英]How to compare the contents of two JSON files with TypeScript?

How can I compare the contents of two JSON files in Angular and achieve the following result: 如何在Angular中比较两个JSON文件的内容并达到以下结果:

Let's say that I have a default.json file that looks like this: 假设我有一个default.json文件,看起来像这样:

default.json default.json

 {
  "name": "John"
  "age": 30,
  "car1": "Ford"
  "car2": "BMW"
  "car3": "Fiat"
 }

And another specific.json file that looks like this: 另一个如下所示的specific.json文件:

specific.json specific.json

 {
  "name": "Doe",
  "car1": "Tesla",
  "car4": "Mercedes"
 }

The default.json contains always all possible keys and the specific.json the keys, which should be overwritten and additional keys, which are not contained in the default.json (in my example: "car4"). default.json始终包含所有可能的键,而specific.json则包含所有键,这些键应被覆盖,而其他键则不包含在default.json中 (在我的示例中为“ car4”)。

Now the following should be checked by the comparison: 现在,应该通过比较检查以下内容:

If a specific key (in my example "name" and "car1") has a different value in the specific.json than in the default.json for the same keys, then these values ​​should be overwritten and used. 如果对于相同的键,特定键(在我的示例中为“ name”和“ car1”)specific.json中的值与default.json中的值不同,则应覆盖并使用这些值。 Otherwise always the default values ​​from the default.json and the additional values ​​from the specific.json of course (in my example "car4") . 否则,当然总是来自default.json的默认值和来自specific.json的其他值(在我的示例中为“ car4”)

At the end both files should be used, so the specific.json serves only as an update or extension of the default.json. 最后,应该使用两个文件,因此,specific.json仅用作default.json的更新或扩展。 I need this functionality in an Angular app, where I have to compare two JSON files and then use the ngx-translate library to provide specific translations based on specific business cases of the application. 我在Angular应用程序中需要此功能,在这里我必须比较两个JSON文件,然后使用ngx-translate库根据应用程序的特定业务案例提供特定的翻译。 Check also my question about this topic please if you want: Translations based on specific keys in custom JSON files and business cases with ngx-translate (Angular 7) 如果需要,也请检查我有关此主题的问题: 基于ngx-translate(Angular 7)的自定义JSON文件和业务案例中特定键的翻译

I hope I could explain it well :) 我希望我能解释清楚:)

This is not related with Angular, but with JS. 这与Angular无关,但与JS有关。

Below example how to do it with objects. 下面的示例如何使用对象。

 let def = { "name": "John", "age": 30, "car1": "Ford", "car2": "BMW", "car3": "Fiat" }; let specific = { "name": "Doe", "car1": "Tesla", "car4": "Mercedes" } function compose(def, spec) { return Object.assign(def, spec); } console.log(compose(def, specific)); 

Output: 输出:

/*
{ 
  name: 'Doe',
  age: 30,
  car1: 'Tesla',
  car2: 'BMW',
  car3: 'Fiat',
  car4: 'Mercedes' 
}
*/

Use es6 spread operator to merge (And override) objects 使用es6传播运算符合并(并覆盖)对象

const defaultObj = {
  name: 'John',
  age: 30,
  car1: 'Ford',
  car2: 'BMW',
  car3: 'Fiat'
};

const specificObj = {
  name: 'Doe',
  car1: 'Tesla',
  car4: 'Mercedes'
}

console.log({ ...defaultObj, ...specificObj });

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

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