使用变量过滤JSON数据

时间:2018-04-09 13:02:18

标签: json node.js amazon-web-services es6-promise

因此,当数据进入时,它会被过滤到某一天。例如:

data.Item.Tuesday 

但是我希望能够将“Tuesday”替换为在其他地方声明的变量。我似乎无法找出这种过滤方法实际上是什么。任何建议都非常感谢!

getWeekData(UserId, weekNumber).then((data) => {
if (!data.Item || !data.Item.weekTotal) {
  console.log("Week Data Not Found");
} else {
  weekExercise = data.Item.weekTotal;
  dayTotal = data.Item.Monday;
  yesterdayTotal = data.Item.Tuesday;
  console.log("Week Total is: " + weekExercise);
  console.log("Todays Total is: " + dayTotal);
  console.log("Yesterdays Total was: " + yesterdayTotal);
}
});

1 个答案:

答案 0 :(得分:1)

您可以像调用数组一样调用对象来替换tuesday

const day_variable = 'tuesday'
data.Item[day_variable]