【问题标题】:Using JavaScript, how to traverse 2 JSON objects, compare their fields and create a new JSON object based on the comparison?使用 JavaScript,如何遍历 2 个 JSON 对象,比较它们的字段并根据比较创建一个新的 JSON 对象?
【发布时间】:2021-08-26 11:37:43
【问题描述】:

我的应用程序获得了 2 个 JSON(我已更改了一些隐私值,但未更改密钥)对象作为来自不同端点的响应。

对象 1

{
   "data":{
      "posts":{
         "edges":[
            {
               "node":{
                  "id":"cG9zdDoyOA==",
                  "date":"2021-08-14T04:10:10",
                  "title":"At vero eos et",
                  "slug":"At vero eos et",
                  "extraPostInfo":{
                     "authorExcerpt":"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.",
                     "thumbImage":"https://res.cloudinary.com/image.jpg",
                     "previewImage":"https://res.cloudinary.com/image.jpg"
                  },
                  "author":{
                     "node":{
                        "name":"et accusamus",
                        "id":"dXNlcjoz"
                     }
                  }
               }
            },
            {
               "node":{
                  "id":"cG9zdDoyMA==",
                  "date":"2021-08-07T07:06:15",
                  "title":"At vero eos et",
                  "slug":"At vero eos et",
                  "extraPostInfo":{
                     "authorExcerpt":"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.",
                     "thumbImage":"https://preview.redd.it/w3kr4m2fi3111.png?auto=webp&s=b4fb4bdfd262de01e49b9f7463d784c6d9013a1b",
                     "previewImage":"https://preview.redd.it/w3kr4m2fi3111.png?auto=webp&s=b4fb4bdfd262de01e49b9f7463d784c6d9013a1b"
                  },
                  "author":{
                     "node":{
                        "name":"et accusamus",
                        "id":"dXNlcjoy"
                     }
                  }
               }
            }
         ]
      }
   },
   "extensions":{
      "debug":[
         {
            "type":"DEBUG_LOGS_INACTIVE",
            "message":"GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled."
         }
      ]
   }
}

对象 2

{
   "usersData":[
      {
         "id":"FJ0i4926gbHnSHAPgF4U",
         "name":"Et harum quidem",
         "imageURL":"https://res.cloudinary.com/image.jpg",
         "role":"author",
         "wpID":"dXNlcjoz"
      },
      {
         "id":"IQxFku4Xhqf5mPmfbYgX",
         "name":"Et harum quidem",
         "role":"author",
         "imageURL":"https://res.cloudinary.com/image.jpg",
         "wpID":"cG9zdDoyMA=="
      }
   ]
}

我想遍历这两个对象,并将 object 1 中的 idobject 2 中的 wpID 进行比较。如果它们相等,我想将 object 2 中的 imageURL(键和值)注入 object 1 edges.author.node 元素并返回一个新的更新 对象 3,它将是 对象 1 的超集,具有 对象 2 的某些值。

*我尝试在两个对象上使用地图功能,但我仍然无法使其工作。任何帮助将不胜感激。如果我确实找到了解决方案,我会发布它。

【问题讨论】:

标签: javascript json api loops javascript-objects


【解决方案1】:

我认为你可以这样做:

obj1.data.posts.edges.forEach(edge => {
  const imageURL = findImage(edge.node.id);
  if (imageURL) {
    edge.node.author.node['imageURL'] = imageURL;
  }
});

function findImage(id) {
  const user = obj2.usersData.find(data => data.wpID === id);
  if (user) {
    return user.imageURL;
  } else {
    return '';
  }
}

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 2016-01-15
    • 2011-05-26
    • 1970-01-01
    • 2018-06-05
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多