Javascript 迭代对象的js数组,在每个对象中,对照所有其他对象检查对象中的值的唯一性,推到新数组
迭代对象的js数组,然后在每个对象内,对照所有其他对象检查对象中的值的唯一性,并推送到新数组的好方法是什么 我有一个对象数组:Javascript 迭代对象的js数组,在每个对象中,对照所有其他对象检查对象中的值的唯一性,推到新数组,javascript,arrays,Javascript,Arrays,迭代对象的js数组,然后在每个对象内,对照所有其他对象检查对象中的值的唯一性,并推送到新数组的好方法是什么 我有一个对象数组: const weatherArray = [ { dt: 1526871600 dt_txt: "2018-05-21 03:22:00" }, { dt: 1526871600 dt_txt: "2018-05-22 03:30:00" }, { dt: 1526871600 dt_txt: "201
const weatherArray = [
{
dt: 1526871600
dt_txt: "2018-05-21 03:22:00"
},
{
dt: 1526871600
dt_txt: "2018-05-22 03:30:00"
},
{
dt: 1526871600
dt_txt: "2018-05-21 03:50:00"
},
{
dt: 1526871600
dt_txt: "2018-05-23 03:17:00"
},
{
dt: 1526871600
dt_txt: "2018-05-23 03:23:00"
}
]
我需要检查每个对象,如果dt_txt(只是日期,而不是时间,例如:2018-05-23、2018-05-21等)是唯一的,则将该对象推到一个新数组中
下面是我一直在尝试的,我对代码进行了注释以显示我的思路
var uniqueDays = []
function getDays(weatherArray) {
// push first value to new array to compare against other items
uniqueDays.push(weatherArray[0])
// get just the yyyy-mm-dd from txt we're comparing against
let firstDayString = weatherArray[0].dt_txt.split(" ")[0]
weatherArray.map((day) => {
let dayString = day.dt_txt.split(" ")[0]
uniqueDays.map((uniqueDay, index) => {
// get just the yyyy-mm-dd for new array items
let unqiueDayString = uniqueDay.dt_txt.split(" ")[0]
// if the value exists, do nothing
if (unqiueDayString == dayString) {
console.log('duplicate');
} else {
// otherwise push to new array (this is the issue)
uniqueDays.push(day)
}
})
})
return uniqueDays
}
我遇到的问题是,在它自己的map函数中推到unique day会导致递归问题。我知道一定有更好的办法。任何帮助或指导都会让我松一口气,我已经为此奋斗了一段时间。您可以使用
将对象分组到一个数组中。reduce
,将每个唯一的日期放入一个按日期索引的对象中,然后获取该对象的值:
const weatherArray=[{dt:1526871600,dtu-txt:“2018-05-21 03:22:00”},{dt:1526871600,dtu-txt:“2018-05-22 03:30:00”},{dt:1526871600,dt-txt:“2018-05-23 03:17:00”},{dt dt dt:1526871600,dt-txt:“2018-05-23:23:00”};
常量输出=
对象值(
weatherArray.reduce((累计,obj)=>{
const date=obj.dt_txt.slice(0,10);
如果(!accum[date])accum[date]=obj;
返回累计;
}, {})
);
控制台日志(输出)
您可以使用对数组中的对象进行分组。reduce
,将每个唯一的日期放入按日期索引的对象中,然后获取该对象的值:
const weatherArray=[{dt:1526871600,dtu-txt:“2018-05-21 03:22:00”},{dt:1526871600,dtu-txt:“2018-05-22 03:30:00”},{dt:1526871600,dt-txt:“2018-05-23 03:17:00”},{dt dt dt:1526871600,dt-txt:“2018-05-23:23:00”};
常量输出=
对象值(
weatherArray.reduce((累计,obj)=>{
const date=obj.dt_txt.slice(0,10);
如果(!accum[date])accum[date]=obj;
返回累计;
}, {})
);
控制台日志(输出)代码>
基本上,伪代码如下所示:
- 环通阵列
- 对于每个子数组,检查新数组中是否存在日期
- 如果不存在,则将此对象推入新数组
- 否则什么也不做
基本上,伪代码如下所示:
- 环通阵列
- 对于每个子数组,检查新数组中是否存在日期
- 如果不存在,则将此对象推入新数组
- 否则什么也不做
const weatherArray=[
{
dt:1526871600,
dt_txt:“2018-05-21 03:22:00”
},
{
dt:1526871600,
dt_txt:“2018-05-22 03:30:00”
},
{
dt:1526871600,
dt_txt:“2018-05-21 03:50:00”
},
{
dt:1526871600,
dt_txt:“2018-05-23 03:17:00”
},
{
dt:1526871600,
dt_txt:“2018-05-23 03:23:00”
}
]
const seen={};
常数res=weatherArray.filter(函数(dt){
const date=dt.dt_txt.substring(0,10);
如果(!已看到[日期]){
看见[日期]=正确;
返回true;
}
});
控制台日志(res)代码>
const weatherArray=[
{
dt:1526871600,
dt_txt:“2018-05-21 03:22:00”
},
{
dt:1526871600,
dt_txt:“2018-05-22 03:30:00”
},
{
dt:1526871600,
dt_txt:“2018-05-21 03:50:00”
},
{
dt:1526871600,
dt_txt:“2018-05-23 03:17:00”
},
{
dt:1526871600,
dt_txt:“2018-05-23 03:23:00”
}
]
const seen={};
常数res=weatherArray.filter(函数(dt){
const date=dt.dt_txt.substring(0,10);
如果(!已看到[日期]){
看见[日期]=正确;
返回true;
}
});
控制台日志(res)代码>这么漂亮的函数。谢谢你的慷慨帮助我刚刚又研究了一遍,想再次感谢你抽出时间。你真的帮助了我在这里成长。多么美好的功能。谢谢你的慷慨帮助我刚刚又研究了一遍,想再次感谢你抽出时间。你真的帮助我在这里成长。
const newUniqWeatherArray = [];
weatherArray.forEach(x => {
const date = x.dt_txt.split(' ')[0];
if(!newUniqWeatherArray.some((x) => x.dt_txt.split(' ')[0] === date)){
newUniqWeatherArray.push(x);
}
})