javascript
JS 数据处理技巧及小算法汇总(转载)
1、根據(jù)屬性來(lái)更新一個(gè)數(shù)組中的對(duì)象
const arr = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}]; //更新的值 const newValue = {id: 3, score: 3}?
更新數(shù)組中id為3的score值。
Es6 裝逼寫(xiě)法如下:
const result = initial.map(x => x.id === newValue.id ? newValue : x); //是不是很裝B??console.log(updated) // => [ { id: 1, score: 1 }, { id: 2, score: 2 }, { id: 3, score: 3 } ]
首先數(shù)組是利用數(shù)組map方法去遍歷arr的每一個(gè)值,然后進(jìn)行于newValue的id進(jìn)行對(duì)比,不同返回原來(lái)的項(xiàng),相同返回newValue.
不裝逼清晰點(diǎn)寫(xiě)法:
const updated = arr.map(function(item){return item.id == newValue.id ? newValue : item ; });console.log(updated) // => [ { id: 1, score: 1 }, { id: 2, score: 2 }, { id: 3, score: 3 } ]
? ?2、數(shù)組去重
對(duì)于大神來(lái)說(shuō),我有一百種方法處理這個(gè)問(wèn)題。
?
?方案 A
// 遍歷數(shù)組,建立新數(shù)組,利用indexOf判斷是否存在于新數(shù)組中,不存在則push到新數(shù)組,最后返回新數(shù)組 function unique(ar) {var ret = [];for (var i = 0, j = ar.length; i < j; i++) {if (ret.indexOf(ar[i]) === -1) {ret.push(ar[i]);}}return ret; }方案B
//遍歷數(shù)組,利用object對(duì)象保存數(shù)組值,判斷數(shù)組值是否已經(jīng)保存在object中,未保存則push到新數(shù)組并用object[arrayItem]=1的方式記錄保存,這個(gè)效率比A高 function unique(ar) {var tmp = {},ret = [];for (var i = 0, j = ar.length; i < j; i++) {if (!tmp[ar[i]]) {tmp[ar[i]] = 1;ret.push(ar[i]);}}return ret; }方案C
//ES6 const numbers = [1, 2, 1, 1, 2, 1, 3, 4, 1 ]; const uniq = [...new Set(numbers)] // => [ 1, 2, 3, 4 ]; const uniq2 = Array.from(new Set(numbers)) // => [ 1, 2, 3, 4 ];稍等,我還有方案D
方案D
//filter function unique (arr) {var res = arr.filter(function (item, index, array) {return array.indexOf(item) === index; //array.indexOf(item) === index 說(shuō)明這個(gè)元素第一次出現(xiàn),后面這個(gè)item再出現(xiàn)他的item肯定不是index了 }) return res; }? ?歡迎大家留言區(qū)給出剩下96種方案。
?
三、根據(jù)屬性刪除數(shù)組中的一個(gè)對(duì)象
// 根據(jù)屬性刪除數(shù)組中的對(duì)象,利用filter進(jìn)行過(guò)濾數(shù)組中id相同的項(xiàng)const initial = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}];const removeId = 3;const without3 = initial.filter(x => x.id !== removeId);console.log(without3) // => [ { id: 1, score: 1 }, { id: 2, score: 2 } ]四、刪除一個(gè)對(duì)象上的屬性(key)
//利用es6的 ...運(yùn)算符將其他屬性和a屬性分開(kāi)來(lái),這波操作很亮眼 ! const obj = {a: 1, b: 2, c: 3}; const {a, ...newObj} = obj; console.log(newObj) // => {b: 2, c: 3};? ? ?
五、兩個(gè)Set對(duì)象相減
//利用filter對(duì)s1進(jìn)行過(guò)濾 ,去掉s2中存在的數(shù)字 const s1 = [ 1, 2, 3, 4, 5 ];const s2 = [ 2, 4 ];const subtracted = s1.filter(x => s2.indexOf(x) < 0);console.log(subtracted);//[1,3,5]?同理這樣是可以去出一個(gè)數(shù)組中指定的元素
//去掉s3中的2和4const s3 = [ 1, 2, 3, 4, 5, 4, 5, 6, 2, 2, 4 ];const s2 = [ 2, 4 ];const subtracted1 = s3.filter(x => s2.indexOf(x) < 0);console.log(subtracted1);?// [1, 3, 5, 5, 6]六、判斷一個(gè)單詞是否是回文
回文是指把相同的詞匯或句子,在下文中調(diào)換位置或顛倒過(guò)來(lái),產(chǎn)生首尾回環(huán)的情趣,叫做回文。例如 12345654321? abcdedbcba 等。
//利用reverse 進(jìn)行字符串反轉(zhuǎn),然后和原字符串對(duì)比是否相等 function isPalindrom(str) { return str == str.split('').reverse().join(''); }七、統(tǒng)計(jì)一個(gè)字符串出現(xiàn)最多的字母
//統(tǒng)計(jì)每個(gè)字母出現(xiàn)的次數(shù),然后存起來(lái),然后進(jìn)行比較 function maxTimesChar(str) { if(str.length == 1) {return str;}let charObj = {};for(let i=0;i<str.length;i++) {if(!charObj[str.charAt(i)]) {charObj[str.charAt(i)] = 1;}else{charObj[str.charAt(i)] += 1;}}let maxChar = '',maxValue = 1;for(var k in charObj) {if(charObj[k] >= maxValue) {maxChar = k;maxValue = charObj[k];}}return maxChar;}總結(jié)
以上是生活随笔為你收集整理的JS 数据处理技巧及小算法汇总(转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vue调用百度地图API
- 下一篇: JavaScript 字符串处理方法总结