从嵌套对象获取所有唯一键

时间:2018-12-06 03:40:26

标签: javascript object unique lodash

我有这个对象:

{
  "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"
      }
    }
  }
}

我想要的是两个唯一的时间戳数组:一个用于apple,一个用于team。 所以:

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"]

如您所见,Apple和团队内部的键不是连续的。 我怎样才能做到这一点?我认为的唯一方法是使用find,但我确定我错了。

(我也可以使用 Lodash

3 个答案:

答案 0 :(得分:1)

也许是这样的:

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.valuesObject.keys为遍历这样的对象提供了很好的方法。 Set是确保唯一性的规范方法。

答案 1 :(得分:0)

您可以参考以下代码。如果有多个对象(例如apples),则可以将代码移至某个函数,而不必一次又一次地编写代码。

var 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"
      }
    }
  }
};

var k = [];
Object.keys(obj.apple).forEach((el)=>{
	k = k.concat(Object.keys(obj.apple[el]));
});
var appleTimeStamps = new Set(k);
appleTimeStamps = Array.from(appleTimeStamps);
console.log(appleTimeStamps);

var j = [];
Object.keys(obj.team).forEach((el)=>{
	j = j.concat(Object.keys(obj.team[el]));
});
var teamTimeStamps = new Set(j);
teamTimeStamps = Array.from(teamTimeStamps);
console.log(teamTimeStamps);

答案 2 :(得分:0)

使用lodash,您可以使用_.flow()创建函数:

  1. _.flatMap()_.keys()一起获得一组时间戳记
  2. _.uniq()删除重复项

const timestamps = _.flow([
  o => _.flatMap(o, _.keys),
  _.uniq,
])

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))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>