var a1 = {}, b1 = '123', c1 = 123;
a1[b1] = 'b';
a1[c1] = 'c';
console.log(a1[b1]);
var a2 = {}, b2 = Symbol('123'), c2 = Symbol('123');
a2[b2] = 'b';
a2[c2] = 'c';
console.log(a2[b2]);
var a3 = {}, b3 = {key: '123'}, c3 = {key: '456'};
a3[b3] = 'b';
a3[c3] = 'c';
console.log(a3[b3]);
/**
result:
c, b, c
主要考点:
对象的 key 值只能为字符串、Symbol
*/
找出字符串数组中,每一项最大的公共前缀
- 假设第一个元素为最大的公共前缀,
- 然后与数组的下一个元素进行对比,得出最大的公共前缀,覆盖之前的最大的公共前缀
- 重复第 2 步,一次对比到数组的最后一个元素
const arr = ['aaaf', 'aadc', 'ad', 'a', 'abf']
function longest(arr) {
let result = ''
function _transform(publicStr, str) {
let result = ''
for (let i = 0; i < publicStr.length; i++) {
const currentStr = publicStr[i]
if (currentStr === str[i]) {
result += currentStr
} else {
break
}
}
return result
}
for (let i = 0; i < arr.length; i++) {
const currentStr = arr[i]
if (currentStr === '') return ''
if (i === 0) {
result = currentStr
continue
}
result = _transform(result, currentStr)
}
return result
}
console.log(longest(arr))