JSFiddle - React, Tailwind, and code Playground
by HYEONGJINKIM
JavaScript
function makeArray(obj) {
if (obj) {
if (Array.isArray(obj)) return obj
return [obj]
}
return [];
}
function flatten(arr) {
var newarr = [];
for (var i = 0, il = arr.length; i < il; i++) {
if (arr[i] instanceof Array) {
newarr = newarr.concat(flatten(arr[i]));
} else {
newarr.push(arr[i]);
}
}
return newarr;
}
function checkIsTrainStation(categories) {
const TRAIN_CID = '226212'
const KTX_CID = '226213'
const SRT_CID = '230819'
const trainStationRegex = new RegExp([TRAIN_CID, KTX_CID, SRT_CID].join('|'))
console.log('trainStationRegex: ', trainStationRegex)
const categoryArray = makeArray(categories.category)
console.log('categoryArray: ', categoryArray)
const isTrainStation = categoryArray.some((category) => trainStationRegex.test(category.id_path))
return isTrainStation
}
const business = {
categories: {
count: 2,
category: [{
id_path: '226187>226188>226192>226213',
name: 'KTX정차역',
path: '기차,철도>KTX정차역',
},
{
id_path: '226187>226188>226192>226212>226265',
name: '경부선',
path: '교통,운수>기차역',
},
],
},
}
const isTrainStation = checkIsTrainStation(business.categories);
console.log(isTrainStation);
const normal = [
[
"226187",
"226188",
"226192",
"226212",
"226287"
]
];
const ktx = [
[
"226187",
"226188",
"226192",
"226213"
],
[
"226187",
"226188",
"226192",
"226212",
"226265"
]
];
const srt = [
[
"226187",
"226188",
"226192",
"230819"
],
[
"226187",
"226188",
"226192",
"226212",
"1001996"
]
]
console.log(flatten(ktx));
console.log(flatten(srt));
console.log(flatten(normal));