我是一个对象数组。每个对象都有一个日期属性和一个字符串属性。我也有一个空数组。我无法弄清楚将基于日期oldest到latest的字符串推送的逻辑。
const oldToNew = []
for (const baseId in results[key][test]) {
// log the array of objects
//example [{string: 'test', date: '2019-03-04T10:36:37.206000Z'}, {string: 'test1', date: '2019-03-010T10:36:37.206000Z'}]
console.log(results[key][test][baseId])
results[key][test][baseId].forEach(element => {
});
}
// I want the value to be [test, test1]
最佳答案
您需要使用 sort
对初始数组进行排序,然后使用 map
提取字符串
像这样:
array.sort((a, b) => a.date < b.date).map(el => el.string);
关于javascript - 根据最旧到最新的日期将对象插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340460/