3 回答
TA贡献2003条经验 获得超2个赞
var mainDateArray = ["05-May-19", "06-May-19", "07-May-19", "08-May-19", "09-May-19", "10-May-19", "11-May-19"]
var dates = ["06-May-19", "08-May-19", "10-May-19"]
var recordsCount = [20, 30, 10]
var result = mainDateArray.map((v, i) => recordsCount[dates.indexOf(v)] || 0)
console.log(result)
TA贡献1797条经验 获得超6个赞
您可以创建->的Map,然后在数组上创建Array#map,检查日期是否存在于地图中。date
recordsCount
mainDateArray
const mainDateArray = [
"05-May-19",
"06-May-19",
"07-May-19",
"08-May-19",
"09-May-19",
"10-May-19",
"11-May-19"
];
const dates = ["06-May-19", "08-May-19", "10-May-19"]; // response date
const recordsCount = [20, 30, 10]; // data for the above dates Array from response
const datesMap = new Map(dates.map((date, idx) => [date, recordsCount[idx]]));
const op = mainDateArray.map(date =>
datesMap.has(date) ? datesMap.get(date) : 0
);
console.log(op);
TA贡献1829条经验 获得超6个赞
你可以试试这个
mainDateArray = [
"05-May-19",
"06-May-19",
"07-May-19",
"08-May-19",
"09-May-19",
"10-May-19",
"11-May-19"
];
dates = ["06-May-19", "08-May-19", "10-May-19"]; // response date
recordsCount = [20, 30, 10];
op = [];
for (let i = 0; i < dates.length; i++) {
for (let j = 0; j < mainDateArray.length; j++) {
if (dates[i] === mainDateArray[j]) {
op[j] = recordsCount[i];
} else {
if (!op[j]) op[j] = 0;
}
}
}
添加回答
举报