携程春招题目字符串截取和数组升维

基本+复杂类型去重:

function unique(arr) {

var hash = {};

return arr.filter(function (element) {

if (hash.hasOwnProperty(element)) {

return false;

}

hash[element] = true;

return true;

});

}

由于数字去重(str,基本类型)基于数组去重,我们要对原来的数组去重做一点修改:

function uniqueStr(str) {

var arr = str.split(‘’);

return arr.filter(function (element, index) {

return arr.indexOf(element) === index;

}).join(‘’);

}

string.split() 和 array.join() 帮助我们自由的游走在字符串和数组间。

最终解答

function handleStr(str) {

var arr = str.split(‘’);

var nums = ‘’;

var words = ‘’;

arr.forEach(function (element) {

if (/\d/.test(element)) {

nums += element;

} else if (/[a-zA-Z]/.test(element) ) {

words += element;

}

});

return uniqueStr(nums) + words;

}

function uniqueStr(str) {

var arr = str.split(‘’);

return arr.filter(function (element, index) {

return arr.indexOf(element) === index;

}).join(‘’);

}

// 测试

console.log(handleStr(‘携程C2t0r1i8p2020校招’));

// 2018Ctrip

2. 数组升维


题目

描述

对一维数组,根据 type 类型分组成二维数组

输入
  • 输入的参数可能是空数组 [],空对象 nullundefined,数字,字符串等异常值;

  • 也可能是结构为 [{ type, content}] 的有效值;

  • 甚至是 [null, null, (type, content)] 等有效和非法值混合的数据。

输出
  • 当输入数据不合法时,输出空数组 []

  • 当输入数据有效时(请先过滤数组里的异常元素),然后将相同 type 值的元素合并,形成新元素{"type": "A", "contents": [content1, content2]},其中,contents 为一个数组,元素为所有 type 值相同的 content 值。

  • 注意,输出的是一个标准 JSON 格式

样例输入

var input = [null, 2, “test”, undefined, {

“type”: “product”,

“content”: “product1”

}, {

“type”: “product”,

“content”: “product2”

}, {

“type”: “tag”,

“content”: “tag1”

}, {

“type”: “product”,

“content”: “product3”

}, {

“type”: “tag”,

“content”: “tag2”

}];

样例输出

[{“type”:“product”,“contents”:[“product1”,“product2”,“product3”]},{“type”:“tag”,“contents”:[“tag1”,“tag2”]}]

解答

乍一看要求颇多,我们一点点来拆解:

条件 1

当输入数据不合法时,输出空数组 []

什么数据不合法?输入值不为 JSON 格式(即 array 类型)

还有呢?输入值为 JSON 格式(即 array 类型),但长度为 0;

由此写下第一句:

function groupList(list) {

if (!Array.isArray(list) || list.length === 0) { return []; }

}

条件 2

当输入数据有效时(请先过滤数组里的异常元素)

过滤掉[],空对象 nullundefined,数字,字符串等异常元素:

function groupList(list) {

if (!Array.isArray(list) || list.length === 0) { return []; }

var validItems = getValidItems(list);

}

function getValidItems(json) {

return json.filter(function (element) {

return isPureObject(element)

});

}

function isPureObject(item) {

return Object.prototype.toString.call(item).slice(8, -1) === ‘Object’ && item !== null ? true : false;

}

条件 3(隐藏条件)

等等,结构不为 { "type": "xx", "content": "yy" } 的值,是不是也为异常元素呢?为此在 getValidItems 里加上一句:

function getValidItems(json) {

return json.filter(function (element) {

return isPureObject(element) && element.type && element.content;

});

}

条件 4

过滤完成后,将相同 type 值的元素合并,形成新元素

function groupList(list) {

if (!Array.isArray(list) || list.length === 0) { return []; }

var validItems = getValidItems(list);

var result = {};

validItems.forEach(function (item) {

if (result.hasOwnProperty(item.type)) {

result[item.type].push(item.content);

} else {

result[item.type] = [];

result[item.type].push(item.content);

}

});

return …;

}

条件 5

貌似我们已经完成了将相同 type 值合并这一步骤,但是

注意,输出的是一个标准 JSON 格式

而且当前的结构是 {type1: contentsArr1, type2: contentsArr2} 的结构,与题目要求的:[ {type1: contentsArr1}, {type1: contentsArr2}] 不相同。

不难,再加一步便是:

function jsonResultGenerator(obj) {

var result = [];

Object.keys(obj).forEach(function (key) {

result.push({ type: key, contents: obj[key] });

});

return result;

}

最终解答

完整的代码:

function groupList(list) {

if (!Array.isArray(list) || list.length === 0) { return []; }

var validItems = getValidItems(list);

var result = {};

validItems.forEach(function (item) {

if (result.hasOwnProperty(item.type)) {

result[item.type].push(item.content);

} else {

result[item.type] = [];

result[item.type].push(item.content);

}

});

return jsonResultGenerator(result);

}

function getValidItems(json) {

return json.filter(function (element) {

return isPureObject(element) && element.type && element.content;

});

}

function isPureObject(item) {

return Object.prototype.toString.call(item).slice(8, -1) === ‘Object’ && item !== null ? true : false;

}

function jsonResultGenerator(obj) {

var result = [];

Object.keys(obj).forEach(function (key) {

result.push({ type: key, contents: obj[key] });

});

return result;

}

// test

var input = [null, 2, “test”, undefined, {

“type”: “product”,

“content”: “product1”

},  {

“type”: “product”,

“content”: “product2”

},  {

“type”: “tag”,

“content”: “tag1”

}, {

“type”: “product”,

“content”: “product3”

}, {

“type”: “tag”,

“content”: “tag2”

}];

console.log(JSON.stringify(groupList(input)));

// [{“type”:“product”,“contents”:[“product1”,“product2”,“product3”]},{“type”:“tag”,“contents”:[“tag1”,“tag2”]}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值