我有一个包含以下值的 structuralFormData
数组:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
},
{
"season": "christmas",
"firstContactPersonName": "Tommy",
"firstContactPersonPhone": "46442644",
"secondContactPersonPhone": "Thomas",
},
]
我有另一个数组保存日期,selectedDate
数组:["autumn=2020-08-28", "christmas=2020-12-28"]
我的函数创建一个新数组updatedStructuredFormData
,并用数据填充新数组以发送到后端。或者也许只是更新现有数组structuralFormData
?
let updatedStructuredFormData = structuredFormData.map(x => {
let season = x.season;
x.date = selectedDate.find(e => e.indexOf(season) > -1).split("=")[1];
return x;
});
问题是,如果我只选择一个日期,并且 dates
数组如下所示:["autumn=2020-08-28"]
我只想将这些值填充到新数组中:
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
}
我是 JavaScript 新手,需要帮助如何仅过滤 dates
数组中的选定值。
预期结果应该是:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
"date": "2020-08-28"
}
]
如果 selectedDate
数组也有圣诞节日期 ["autumn=28-08-28", "christmas=2020-12-28"]
,则预期结果将是:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
"date": "2020-08-28"
},
{
"season": "christmas",
"firstContactPersonName": "Tommy",
"firstContactPersonPhone": "46442644",
"date": "2020-12-28"
}
]
谢谢:)
最佳答案
下面是使用 .reduce()
执行此操作的一种方法:
// Array to filter
const arr = [
{
season: "autumn",
firstContactPersonName: "John",
firstContactPersonPhone: "46442644",
secondContactPersonName: "Jhonny",
secondContactPersonPhone: "46442644"
},
{
season: "christmas",
firstContactPersonName: "Tommy",
firstContactPersonPhone: "46442644",
secondContactPersonPhone: "Thomas"
}
];
// date to filter for
const selectedDates = ["autumn=28-08-28", "christmas=2020-12-28"];
// loop over each item and filter for season that is in "selectedDate"
const filtered = arr.reduce((acc, curr) => {
// looping over dates to see if current items date is in the selected dates. We also save the result to a const as we will use it to add to our final array
const maybeDate = selectedDates.filter(date => date.includes(curr.season));
// check if we have a date
if (maybeDate.length > 0) {
// extract date
const date = maybeDate[0].split("=")[1];
// update accumulator
acc = [...acc, { ...curr, date }];
}
return acc;
}, []);
console.log({ filtered });
// results
{
filtered:
[{
season: 'autumn',
firstContactPersonName: 'John',
firstContactPersonPhone: '46442644',
secondContactPersonName: 'Jhonny',
secondContactPersonPhone: '46442644',
date: '28-08-28'
},
{
season: 'christmas',
firstContactPersonName: 'Tommy',
firstContactPersonPhone: '46442644',
secondContactPersonPhone: 'Thomas',
date: '2020-12-28'
}]
}
关于javascript - 如何根据第三个数组从另一个数组创建一个新数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932420/