如何在特定条件下更新JSON对象

时间:2018-07-20 07:45:23

标签: javascript json node.js javascript-objects

我有两个JSON文件,第一个是origin.json,第二个是newData.json

我想基于Origin更新NewData的子值,但是更新必须仅影响userAgents的{​​{1}}中存在的项目。

以下代码展示了我需要转换的数据:

newData.json

在这种情况下,结果是这样的:

var originData = {
    "userAgents": [
        "Chrome",
        "Safari"
    ],
    "tests": [
        {
            "name": "a",
            "errs": []
        },
        {
            "name": "b",
            "errs": [
                {
                    "userAgent": "Chrome"
                },
                {
                    "userAgent": "Safari"
                }
            ]
        }
    ]
};

var newData = {
    "userAgents": [
        "Chrome"
    ],
    "tests": [
        {
            "name": "a",
            "errs": []
        },
        {
            "name": "b",
            "errs": []
        }
    ]
};

originData.tests.forEach(function (originItem) {
    newData.tests.forEach(function (newItem) {
        if (newItem.name === originItem.name) {
            originItem.errs = newItem.errs
        }
    })


});


console.log(originData);

但是这个结果是错误的,正确的结果应该像这样:

{
    "userAgents": [
        "Chrome",
        "Safari"
    ],
    "tests": [
        {
            "name": "a",
            "errs": []
        },
        {
            "name": "b",
            "errs": []
        }
    ]
}

您是否知道如何通过使用Node.js来获得此结果?

3 个答案:

答案 0 :(得分:0)

使用以下代码:

const originData = {
  "userAgents": [
      "Chrome",
      "Safari"
  ],
  "tests": [
    {
      "name": "a",
      "errs": []
    },
    {
      "name": "b",
      "errs": [
        {
          "userAgent": "Chrome"
        },
        {
          "userAgent": "Safari"
        }
      ]
    }
  ]
};

const newData = {
  "userAgents": [
      "Chrome"
  ],
  "tests": [
    {
      "name": "a",
      "errs": []
    },
    {
      "name": "b",
      "errs": [
        {
          "userAgent": "Safari"
        }
      ]
    }
  ]
};

originData.tests.forEach(originItem => {
  const newEl = newData.tests.find(ne => ne.name === originItem.name);
  originItem.errs = originItem.errs.filter(e => newData.userAgents.includes(e.userAgent) || (newEl && -1 !== newEl.errs.findIndex(nee => nee.userAgent === e.userAgent)));
});


console.log(originData);

答案 1 :(得分:0)

可以使用一些外部模块吗? 例如,可以使用lodash模块https://lodash.com/docs/4.17.10#merge

(顺便说一句:npm https://www.npmjs.com/search?q=deep%20merge中有很多类似的库)

答案 2 :(得分:0)

var originData = {
  "userAgents": [
      "Chrome",
      "Safari"
  ],
  "tests": [
      {
          "name": "a",
          "errs": []
      },
      {
          "name": "b",
          "errs": [
              {
                  "userAgent": "Chrome"
              },
              {
                  "userAgent": "Safari"
              }
          ]
      }
  ]
};

var newData = {
  "userAgents": [
      "Chrome"
  ],
  "tests": [
      {
          "name": "a",
          "errs": []
      },
      {
          "name": "b",
          "errs": [
            {"userAgent": "Chrome"}
          ]
      }
  ]
};

originData.tests.forEach(originItem => {
  originItem.errs = originItem.errs.filter(e => (newData.userAgents.indexOf(e.userAgent) < 0 ));
});

newData.tests.forEach( newDataItem => {
  let originDataItem = originData.tests.filter(obj=>obj.name===newDataItem.name)
  newDataItem.errs.forEach( err => {
    originDataItem[0].errs.push( err )
  })
})


console.log(originData);