Home>

I have the following json data.

{
  "data": [
    {
      "shop": "A",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    },
    {
      "shop": "B",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    }
  ]
}

I want to generate the following two types of data based on the above json data.

  • An array that contains only users who have deduplicated members
newMembers ["user: [email protected]", "user: [email protected]", "user: [email protected]"]
  • Data reconverted to the information of the shop to which the user belongs based on json
newData = {
  "user: [email protected]": [A, B],
  "user: [email protected]": [B],
  "user: [email protected]": [A]
}
Question question 1

I wrote the code to do deduplication from members of json data, but it stores json datarBesides the variablemembersWhennewMembersIt seems that the current code is not very smart in terms of memory usage if the data of r becomes large. If i have any other good ideas, please let me know.

var r = {
  "data": [
    {
      "shop": "A",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    },
    {
      "shop": "B",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    }
  ]
};
var members = [];
for (var i = 0;i<r.data.length;i ++) {
    members = members.concat (r.data [i] ["members"])
}
newMembers = [... new Set (members)]
console.log (newMembers)
Question 2

from jsonnewDataI'm worried because I can't think of a smart code to convert to.
Can you tell me if you have any good ideas?

{
  "data": [
    {
      "shop": "A",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    },
    {
      "shop": "B",
      "members": [
        "user: [email protected]",
        "user: [email protected]"
      ]
    }
  ]
}
newData = {
  "user: [email protected]": [A, B],
  "user: [email protected]": [B],
  "user: [email protected]": [A]
}
  • Answer # 1

    const members = [... new Set (r.data.map (x =>x.members) .flat ())];
    const newData = Object.fromEntries (members.map (x =>[x, r.data.filter (y =>y.members.includes (x)). map (x =>x.shop)]));

  • Answer # 2

    I don't think you need to worry too much about the memory usage in Question 1.

    Question 2 is the following idea.

    var r = {
      "data": [
        {
          "shop": "A",
          "members": [
            "user: [email protected]",
            "user: [email protected]"
          ]
        },
        {
          "shop": "B",
          "members": [
            "user: [email protected]",
            "user: [email protected]"
          ]
        }
      ]
    };
    var newMembers = ["user: [email protected]", "user: [email protected]", "user: [email protected]"];// Results of Question 1
    var getUserShops = (shopList, user) =>shopList.filter (s =>s.members.includes (user)). map (s =>s.shop);
    var newData = Object.fromEntries (newMembers.map (user =>[user, getUserShops (r.data, user)]));
    console.log (newData);