根据匹配值从 2 个数组创建(多维?)数组
Create (Multidimensional ?) Array from 2 arrays based on matching values
我对如何基于其他 2 个数组正确制作数组有点困惑。
我有一个这样的数组:
let date = [1522303200000, 1522389600000, 1522476000000]
第二个数组看起来有点像这样:
let people = [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
},
{
"id": 1,
"name": "Brandon Hampton",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522648800000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
我正在尝试创建一个数组,该数组将日期作为数组中每个索引的顶级,然后如果该日期有空的话,则在每一天的每个对应的人。我在想类似的事情:
let calendar = [
[
1522303200000,
"photographers": [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
},
{
"id": 1,
"name": "Brandon Hampton",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522648800000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
],
[
1522303200000,
"photographers": [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
],
...
]
但我目前还在纠结如何解决这个问题。我可能已经看这个太久了,无法真正解决这个问题。
我查看了以下地方以获得一些想法:
stack
如有任何帮助,我们将不胜感激!
这个函数
createCalendar(date, people) {
const calendar = {};
date.forEach(element => {
calendar[element] = [];
people.forEach(person => {
if (person.availability.map(item => item.date).includes(element)) {
calendar[element].push(person);
}
})
})
return calendar;
}
创建与您所想的相似的数据结构。 calendar
是一个对象,其中每个键是一个 date
,对应的值是一个数组,其中包含该日期可用的所有 people
。
我对如何基于其他 2 个数组正确制作数组有点困惑。
我有一个这样的数组:
let date = [1522303200000, 1522389600000, 1522476000000]
第二个数组看起来有点像这样:
let people = [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
},
{
"id": 1,
"name": "Brandon Hampton",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522648800000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
我正在尝试创建一个数组,该数组将日期作为数组中每个索引的顶级,然后如果该日期有空的话,则在每一天的每个对应的人。我在想类似的事情:
let calendar = [
[
1522303200000,
"photographers": [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
},
{
"id": 1,
"name": "Brandon Hampton",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522648800000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
],
[
1522303200000,
"photographers": [
{
"id": 0,
"name": "Sophia Mason",
"biography": "Lorem ...",
"availability": [
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522389600000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522476000000,
"times": ["10:00 am", "2:30 pm", "5:00 pm"]
},
{
"date": 1522735200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
}
]
}
]
],
...
]
但我目前还在纠结如何解决这个问题。我可能已经看这个太久了,无法真正解决这个问题。
我查看了以下地方以获得一些想法:
stack
如有任何帮助,我们将不胜感激!
这个函数
createCalendar(date, people) {
const calendar = {};
date.forEach(element => {
calendar[element] = [];
people.forEach(person => {
if (person.availability.map(item => item.date).includes(element)) {
calendar[element].push(person);
}
})
})
return calendar;
}
创建与您所想的相似的数据结构。 calendar
是一个对象,其中每个键是一个 date
,对应的值是一个数组,其中包含该日期可用的所有 people
。