JavaScript 数组/字符串 排序、查找、去重

 对数组和字符串排序、查找、去重是常见的操作,这里权做学习记录。

数组和字符串在空间上都可以看作连续的变量,所以两者的排序应该是一致的吧。就像可以将字符串视为包含字符的数组集合,可以使用数组的一些方法和索引操作来处理字符串。例如:使用循环遍历字符串中的字符,或者使用字符串的length属性获取字符串的长度。

数组的类型:

const numbers = [10, 5, 8, 2, 7];


const strings = ['banana', 'cherry', 'apple', 'date'];


const objects= [
{id:1,content:'a'},
{id:5,content:'c'},
{id:4,content:'b'},
{id:2,content:'f'},
{id:3,content:'d'},
];

一、排序

1.sort

这是按照元素的值进行排序,而不是按照元素的类型进行排序。

如果是数字,则是按照值的大小,如果是字符串,则基于元素的 Unicode 编码值(字符编码)。

const numbers = [10, 5, 8, 2, 7];
numbers.sort((a, b) => a - b);
console.log(numbers); // 输出:[2, 5, 7, 8, 10]



const strings = ['banana', 'cherry', 'apple', 'date'];
strings.sort(); // 默认按照 Unicode 编码值排序
console.log(strings); // 输出:['apple', 'banana', 'cherry', 'date']

注意:sort 可以接受一个可选的比较函数,以定义自定义的排序规则 

上面那种,a-b 代表的是升序,反之是降序。(记忆技巧,ABCD的值是依次增大,个人用)

2.sort自定义

 自定义的如下面这种:(返回字符串的长度大小)

const strings = ['banana', 'cherry', 'apple', 'date'];
strings.sort((a, b) => a.length - b.length);
console.log(strings); // 输出:['date', 'apple', 'cherry', 'banana']

当然针对数组里的某个对象的属性也可以通过以下方式进行排序 

const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 22 }
];

people.sort((a, b) => a.age - b.age); // 按年龄升序排序
console.log(people);

二、查找

对于数组,常常可以用这几个方法来实现查找功能:indexOf()方法来在数据库中的索引位置查找某个特定的值,也可以使用find()方法来查找满足特定条件的元素。当然还是要根据查找的复杂性和需求来确认是使用那种

1.indexOf()

查找到符合条件的元素的索引

const fruits = ["apple", "banana", "cherry", "date", "berry"];
const index = fruits.indexOf("cherry");
if (index !== -1) {
  console.log(`"cherry" 的索引是 ${index}`);
} else {
  console.log("未找到");
}

2.filter()

过滤出符合条件的元素。

const numbers = [3, 1, 5, 2, 4];
const evenNumbers = numbers.filter((number) => number % 2 === 0);
console.log("偶数数组:", evenNumbers);

 3.find()

对象数组,找到符合条件的。

const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 22 }
];

const person = people.find((person) => person.name === "Bob");
if (person) {
  console.log(`找到了 ${person.name},年龄是 ${person.age} 岁`);
} else {
  console.log("未找到");
}

三、去重

1.new Set()

改变数据结构,ES6以上支持:

const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // [1, 2, 3, 4, 5]

这种可以支持查找, uniqueArray.has(某个值)

2.filter()

很常用

const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = array.filter((value, index, self) => self.indexOf(value) === index);
console.log(uniqueArray); // [1, 2, 3, 4, 5]

 3.reduce() 

没怎么用过

const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = array.reduce((accumulator, currentValue) => {
  if (!accumulator.includes(currentValue)) {
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);
console.log(uniqueArray); // [1, 2, 3, 4, 5]

4.for循环

最基础的,时间和空间复杂度都是0

const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = [];
for (const item of array) {
  if (!uniqueArray.includes(item)) {
    uniqueArray.push(item);
  }
}
console.log(uniqueArray); // [1, 2, 3, 4, 5]

 5.map()

const array = [1, 2, 2, 3, 4, 4, 5];
const map = new Map();
const uniqueArray = [];
for (const item of array) {
  if (!map.has(item)) {
    map.set(item, true);
    uniqueArray.push(item);
  }
}
console.log(uniqueArray); // [1, 2, 3, 4, 5]

11月7号,暂时先记这些。 

, 2, 3, 4, 5]

总结

这里记录一些开发中常用到的一些数据排序、查找、去重的方法,之后有时间遇到再更新补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值