假设您有以下数组[0,1,2,3,4,5,6]
,0
代表星期天,1
代表星期一……直到6
代表星期六。
我想产生如下字符串输出:
weekdays //[1,2,3,4,5]
weekends //[0,6]
mon, tue, wed //[1,2,3]
它也可以是混合组,
weekends, thu //[0,6,4]
weekdays, sat //[1,2,3,4,5,6]
*已输入注释,输出在左侧。
尝试过片刻,但找不到任何方法来解析它。
答案 0 :(得分:0)
如何?
function getHumanDescription(daysArray) {
const dayNames = [ 'sat', 'mon', 'tue', 'wed', 'thu', 'fri', 'sun' ]
const weekdays = [1,2,3,4,5]
const weekend = [0,6]
let description = []
if(weekdays.every(day => daysArray.includes(day))) {
description.push('weekdays')
daysArray = daysArray.filter(day => !weekdays.includes(day))
}
if(weekend.every(day => daysArray.includes(day))) {
description.push('weekends');
daysArray = daysArray.filter(day => !weekend.includes(day))
}
daysArray.forEach(day => description.push(dayNames[day]))
return description.join(', ')
}
console.log(getHumanDescription([0,1,2,3,4,5,6]));
console.log(getHumanDescription([0,5,6]));
console.log(getHumanDescription([1,2,3,4,5,6]));
console.log(getHumanDescription([1,3,4,5,6]));
答案 1 :(得分:0)
这是我的看法。希望对您有所帮助。
const week = {
SUNDAY: 0,
MONDAY: 1,
TUESDAY: 2,
WEDNESDAY: 3,
THURSDAY: 4,
FRIDAY: 5,
SATURDAY: 6,
weekday: [1,2,3,4,5],
weekend: [0,6]
};
let array1 = [1,2,3,4,5]; //weekdays
let array2 = [0,6]; //weekends
let array3 = [1,2,3]; //mon, tue, wed
let array4 = [0,6,4]; //weekends, thu
let array5 = [1,2,3,4,5,6]; //weekdays, sat
let keys = Object.keys(week);
let values = Object.values(week);
function parseArray(array) {
let isWeekend = week.weekend.every((val) => array.includes(val));
let isWeek = week.weekday.every((val) => array.includes(val));
let foundDays = [];
//console.log("Contains Weekend: " + isWeekend);
//console.log("Contains Week: " + isWeek);
isWeekend ? foundDays.push("Weekend") : null;
isWeek ? foundDays.push("Weekdays") : null;
for (let i = 0; i < array.length; i++) {
let foundIndex = values.indexOf(array[i]);
if (isWeek && foundIndex > 0 && foundIndex < 6) {
continue;
}
if (isWeekend && (foundIndex === 0 || foundIndex === 6)) {
continue;
}
if (foundIndex > -1) {
foundDays.push(keys[foundIndex]);
}
}
console.log(foundDays);
}
parseArray(array1);
parseArray(array2);
parseArray(array3);
parseArray(array4);
parseArray(array5);
答案 2 :(得分:0)
我认为您暂时无法做到这一点。我将创建一个函数来为您执行此操作。 像下面这样。
function getDays(input) {
const ref = {
weekdays: [1, 2, 3, 4, 5],
weekends: [0, 6],
sun: [0],
mon: [1],
tue: [2],
wed: [3],
thu: [4],
fri: [5],
sat: [6]
}
let output = []; //this will hold array of output days
//Check for each combination in ref object
for (prop in ref) {
let isPresent = ref[prop].every(item => {
return input.indexOf(item) !== -1;
});
//if the combination is present add to output and remove those days from input
if (isPresent) {
output.push(prop);
input = input.reduce((acc, value) => {
if (ref[prop].indexOf(value) !== -1) {
return acc;
} else {
acc.push(value);
return acc;
}
}, []);
}
}
return output.join(', ');
}
console.log(getDays([1, 2]));
console.log(getDays([0, 3, 6]));
console.log(getDays([1, 2, 3, 4, 5]));
答案 3 :(得分:0)
简单方法:-
var daysOfWeek = [0,1,2,3,4,5,6,[0,6],[1,2,3,4,5]];
var dayNames = ['sun','mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'weekends','weekdays' ];
function mapDescriptionWithDays(inpString){
inpArray=inpString.split(',');
var output='';
for(i=0; i< inpArray.length ; i++){
output += ','+daysOfWeek[dayNames.indexOf((inpArray[i]).trim())];
}
output=output.substring(1);
output='[' + output +']';
return output;
}
var a = mapDescriptionWithDays('weekdays, sat');
alert(a);