在使用变量名访问对象内的属性时遇到问题

时间:2020-09-02 19:24:54

标签: javascript node.js json

我有一些看起来像这样的数据:

   [
          {
            teamName: '4',
            teamId: 6665,
            position: 1,
            points: 1000,
            posChange: 0,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '3',
            teamId: 6667,
            position: 2,
            points: 735,
            posChange: 0,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '1',
            teamId: 4608,
            position: 3,
            points: 561,
            posChange: 1,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '2',
            teamId: 4494,
            position: 4,
            points: 535,
            posChange: -1,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          }
        ]

我想把它变成这样:

[
   '21-5-2018': [ 
       {
            teamName: '4',
            teamId: 6665,
            position: 1,
            points: 1000,
            posChange: 0,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '3',
            teamId: 6667,
            position: 2,
            points: 735,
            posChange: 0,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '1',
            teamId: 4608,
            position: 3,
            points: 561,
            posChange: 1,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          },
          {
            teamName: '2',
            teamId: 4494,
            position: 4,
            points: 535,
            posChange: -1,
            rankDay: 21,
            rankMonth: 5,
            rankYear: 2018
          }
       ]
]

因此,我想将每个排名实例添加到具有排名日期的对象。这是我到目前为止所拥有的:

// Reading the Json file with the data
let rawRankings = fs.readFileSync('rankings until 07-Sep-2020.json')
let rankings = JSON.parse(rawRankings);
let rankingsNew = [];
// Iterating through each element
rankings.forEach(function(value){
    // This is the name of the object that will group all the values for that date
    let newRankName = value.rankYear + "-" + value.rankMonth + "-" + value.rankDay;
    let length = rankingsNew.length;
    // Now I want to check if the object needs to be created or not, but this doesn't work, it always returns "undefined"
    if(typeof rankingsNew[newRankName] === 'undefined') {
        // Adding the object to the array since it doesn't exist
        rankingsNew.splice(length, 0, newRankName);
        // Trying to add this instance to the new object. Push doesn't work either
        rankingsNew.newRankName = value;
    }
    else {
        // Here I would only add the instance to the object since it already exists. Here is the push version for comparison
        rankingsNew.newRankName.push({
            "teamName" : value.teamName,
            "teamId": value.teamId,
            "position": value.position,
            "points": value.points,
            "posChange": value.posChange,
            "rankDay": value.rankDay,
            "rankMonth": value.rankMonth,
            "rankYear": value.rankYear, 
            "date": value.date
        })
    }
});

所以我需要:

1-检查对象是否存在(排名日期)

2-能够将一个对象添加到一个空的JSON对象(如果尚不存在)(将指定的日期添加为一个内部具有排名的新对象)

3-然后,一旦实例创建/确认存在,则将实例的值添加到该对象(每次发现属于每个对象的排名的迭代都应添加该实例)

有想法吗?

2 个答案:

答案 0 :(得分:1)

尝试一下, 您只是在弄乱对象/数组结构。

df.loc[df[Year'] == df['Year of Death'], 'Event'] = 'Yes'

答案 1 :(得分:0)

您的代码/数据结构概念中存在一些基本错误。您想要将对象数组转换为Hashmap(或TypeScript中的Record ),如下所示:

{
  '21-5-2018': [ 
  //...
  ],
}

显然,使用数组是不可能的,因为数组的数据结构与您要获取的数据结构不同。

解决方案就这么简单:

// Reading the Json file with the data
const rawRankings = fs.readFileSync('rankings until 07-Sep-2020.json')
const rankings = JSON.parse(rawRankings);
const rankingsNew = rankings.reduce((result, current) => {
  const newRankName = value.rankYear + "-" + value.rankMonth + "-" + value.rankDay;
  return {
    ...result,
    [newRankName]: [
      ...(result[newRankName] || []),
      current,
    ],
  };
}, {});