我有一个JS对象需要重新排列才能在HTML表格中正确显示数据。
基本上,我需要从第一个对象集的第一行获取一些属性,并且每个对象集中只有第一个属性来自第一个对象行。这将是新创建的对象中第一个记录的数据。
var testObject =
{
6:{
5:{
0:{ FirstItem: "1-1-1-1", SecondItem: "1-1-1-2", ThirdItem: "1-1-1-3", FourthItem: "1-1-1-4", Year: "2014"},
1:{ FirstItem: "1-1-2-1", SecondItem: "1-1-2-2", ThirdItem: "1-1-2-3", FourthItem: "1-1-2-4", Year: "2014"},
2:{ FirstItem: "1-1-3-1", SecondItem: "1-1-3-2", ThirdItem: "1-1-3-3", FourthItem: "1-1-3-4", Year: "2014"}
},
10:{
0:{ FirstItem: "1-2-1-1", SecondItem: "1-2-1-2", ThirdItem: "1-2-1-3", FourthItem: "1-2-1-4", Year: "2014"},
1:{ FirstItem: "1-2-2-1", SecondItem: "1-2-2-2", ThirdItem: "1-2-2-3", FourthItem: "1-2-2-4", Year: "2014"},
2:{ FirstItem: "1-2-3-1", SecondItem: "1-2-3-2", ThirdItem: "1-2-3-3", FourthItem: "1-2-3-4", Year: "2014"}
}
},
22:{
5:{
0:{ FirstItem: "2-1-1-1", SecondItem: "2-1-1-2", ThirdItem: "2-1-1-3", FourthItem: "2-1-1-4", Year: "2015"},
1:{ FirstItem: "2-1-2-1", SecondItem: "2-1-2-2", ThirdItem: "2-1-2-3", FourthItem: "2-1-2-4", Year: "2015"},
2:{ FirstItem: "2-1-3-1", SecondItem: "2-1-3-2", ThirdItem: "2-1-3-3", FourthItem: "2-1-3-4", Year: "2015"}
},
10:{
0:{ FirstItem: "2-2-1-1", SecondItem: "2-2-1-2", ThirdItem: "2-2-1-3", FourthItem: "2-2-1-4", Year: "2015"},
1:{ FirstItem: "2-2-2-1", SecondItem: "2-2-2-2", ThirdItem: "2-2-2-3", FourthItem: "2-2-2-4", Year: "2015"},
2:{ FirstItem: "2-2-3-1", SecondItem: "2-2-3-2", ThirdItem: "2-2-3-3", FourthItem: "2-2-3-4", Year: "2015"}
}
}
};
所需对象:
var desiredOutput =
{ FirstItem: "1-1-1-1", FourthItem: "1-1-1-4", Year: "2014", FirstItem: "1-2-1-1", FirstItem: "2-1-1-1", FirstItem: "2-2-1-1" },
{ FirstItem: "1-1-2-1", FourthItem: "1-1-2-4", Year: "2014", FirstItem: "1-2-2-1", FirstItem: "2-1-2-1", FirstItem: "2-2-2-1" },
{ FirstItem: "1-1-3-1", FourthItem: "1-1-3-4", Year: "2014", FirstItem: "1-2-3-1", FirstItem: "2-1-3-1", FirstItem: "2-2-3-1" };
请查看随附的图片以便更好地理解。
请检查JSFiddle链接与我当前拥有的对象和所需的输出:
https://jsfiddle.net/fn3epjux/1/
我已经尝试了一切,但没有成功。我在我的项目中使用了underscoreJS。