Vue
HTML
<div id="app">
<div v-for="result in results">
{{ result }}
</div>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
Vue
new Vue({
el: "#app",
data: {
results: [],
testDatas: [
'3:kabe 5:buzz 1',
'3:hoge 5:fuga 3',
'3:piyo 5:fuga 5',
'3:kabe 5:don 15',
'3:fuga 5:piyo 2:hoge 30'
]
},
mounted() {
this.testDatas.forEach(data => {
this.results.push(this.check(data))
})
},
methods: {
check(string) {
const items = string.split(' ')
const length = items.length;
const tempArray = [];
const commonMultiple = items[length - 1];
let result = "";
items.forEach((item, index) => {
const object = item.split(':');
const key = Number(object[0]);
const value = object[1];
if(index < length - 1) {
tempArray.push({'key':key,'value':value})
}
})
tempArray.sort(this.sort);
tempArray.forEach(item => {
if(commonMultiple % item.key === 0) {
if (item.value)
result = item.value ? result + item.value : result;
}
})
if (!result) result = commonMultiple
return result;
},
sort(a, b) {
if (a.key >= b.key) {
return 1;
} else {
return -1;
}
}
}
})