Search code examples
javascriptobjectuniquelodash

Get all unique key from nested object


I have this object:

{
  "apple": {
    "0": {
      "2018-04-25 19:51:38": {
        "x": "38.0",
        "y": "23.0"
      },
      "2018-04-25 19:51:39": {
        "x": "NaN",
        "y": "NaN"
      },
      "2018-04-25 19:51:40": {
        "x": "NaN",
        "y": "NaN"
      }
    },
    "5": {
      "2018-04-25 19:51:38": {
        "x": "50.0",
        "y": "35.0"
      },
      "2018-04-25 19:51:43": {
        "x": "21.0",
        "y": "3.0"
      }
    },
    "6": {
      "2018-04-25 19:51:34": {
        "x": "30.0",
        "y": "15.0"
      },
      "2018-04-25 19:51:39": {
        "x": "NaN",
        "y": "NaN"
      },
      "2018-04-25 19:52:40": {
        "x": "22.0",
        "y": "20.0"
      },
      "2018-04-25 19:52:42": {
        "x": "33.0",
        "y": "45.0"
      }
    }
  },
  "team": {
    "2": {
      "2018-04-25 19:51:35": {
        "x": "32.0",
        "y": "25.0"
      },
      "2018-04-25 19:51:36": {
        "x": "33.0",
        "y": "40.0"
      },
      "2018-04-25 19:51:37": {
        "x": "12.0",
        "y": "24.0"
      },
      "2018-04-25 19:51:38": {
        "x": "33.0",
        "y": "45.0"
      }
    },
    "3": {
      "2018-04-25 19:51:35": {
        "x": "2.0",
        "y": "3.0"
      },
      "2018-04-25 19:51:36": {
        "x": "4.0",
        "y": "5.0"
      },
      "2018-04-25 19:51:37": {
        "x": "12.0",
        "y": "15.0"
      },
      "2018-04-25 19:51:38": {
        "x": "33.0",
        "y": "45.0"
      }
    },
    "4": {
      "2018-04-25 19:51:35": {
        "x": "20.0",
        "y": "30.0"
      },
      "2018-04-25 19:51:36": {
        "x": "41.0",
        "y": "35.0"
      },
      "2018-04-25 19:51:37": {
        "x": "32.0",
        "y": "65.0"
      },
      "2018-04-25 19:51:38": {
        "x": "43.0",
        "y": "49.0"
      }
    },
    "5": {
      "2018-04-25 19:51:35": {
        "x": "21.0",
        "y": "33.0"
      },
      "2018-04-25 19:51:36": {
        "x": "31.0",
        "y": "12.0"
      },
      "2018-04-25 19:51:37": {
        "x": "34.0",
        "y": "54.0"
      },
      "2018-04-25 19:51:38": {
        "x": "93.0",
        "y": "22.0"
      }
    }
  }
}

What I want are two array of unique timestamps: one for apple and one for team. So:

const appleTimestamps = ["2018-04-25 19:51:34", "2018-04-25 19:51:37", "2018-04-25 19:51:38", "2018-04-25 19:51:39", "2018-04-25 19:51:40", "2018-04-25 19:51:43", "2018-04-25 19:52:40", "2018-04-25 19:52:42"]
const teamTimestamps = ["2018-04-25 19:51:35", "2018-04-25 19:51:36", "2018-04-25 19:51:37", "2018-04-25 19:51:38"]

As you can see, keys inside Apple and team are not sequential. How can I do that? The only way i thought is use find but I'm sure I'm wrong.

(I can use also Lodash)


Solution

  • Perhaps something like this:

    const timestamps = obj => 
      [...new Set(
        Object.values(obj)
          .map(Object.keys)
          .reduce((a, b) => a.concat(b), []) // someday: `flatten`
      )]
    
    
    const obj = {"apple": {"0": {"2018-04-25 19:51:38": {"x": "38.0", "y": "23.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:51:40": {"x": "NaN", "y": "NaN"}}, "5": {"2018-04-25 19:51:38": {"x": "50.0", "y": "35.0"}, "2018-04-25 19:51:43": {"x": "21.0", "y": "3.0"}}, "6": {"2018-04-25 19:51:34": {"x": "30.0", "y": "15.0"}, "2018-04-25 19:51:39": {"x": "NaN", "y": "NaN"}, "2018-04-25 19:52:40": {"x": "22.0", "y": "20.0"}, "2018-04-25 19:52:42": {"x": "33.0", "y": "45.0"}}}, "team": {"2": {"2018-04-25 19:51:35": {"x": "32.0", "y": "25.0"}, "2018-04-25 19:51:36": {"x": "33.0", "y": "40.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "24.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "3": {"2018-04-25 19:51:35": {"x": "2.0", "y": "3.0"}, "2018-04-25 19:51:36": {"x": "4.0", "y": "5.0"}, "2018-04-25 19:51:37": {"x": "12.0", "y": "15.0"}, "2018-04-25 19:51:38": {"x": "33.0", "y": "45.0"}}, "4": {"2018-04-25 19:51:35": {"x": "20.0", "y": "30.0"}, "2018-04-25 19:51:36": {"x": "41.0", "y": "35.0"}, "2018-04-25 19:51:37": {"x": "32.0", "y": "65.0"}, "2018-04-25 19:51:38": {"x": "43.0", "y": "49.0"}}, "5": {"2018-04-25 19:51:35": {"x": "21.0", "y": "33.0"}, "2018-04-25 19:51:36": {"x": "31.0", "y": "12.0"}, "2018-04-25 19:51:37": {"x": "34.0", "y": "54.0"}, "2018-04-25 19:51:38": {"x": "93.0", "y": "22.0"}}}}
    
    console.log(timestamps(obj.apple))
    console.log(timestamps(obj.team))

    Object.values and Object.keys make for great ways to traverse objects like this. And a Set is the canonical way to ensure uniqueness.