1、删除数组中指定对象指定元素
let arr = [{
name: "xiaowang",
id: 1
},, {
name: "xiaozhang",
id: 2
}, {
createDate: "xiaoli",
id: 3
}]
//删除id为1的对象,其中i为index,1可选择性填写,含义为删除当前元素后的几个元素
arr.some((item, i) => {
if (item.id === '1') {
arr.splice(i, 1)
}
})
2、删除数组指定元素
var arr = [1,2,3,4,5]
arr.map((val, i) => {
if (val === 1) {
this.caseLabel.splice(i, 1)
}
})
3、去除重复数组对象
this.optdata = Array.from(new Set(this.optdata))
const arr = [
{
name: 'John',
location: 'Los Angeles',
},
{
name: 'Kate',
location: 'New York',
},
{
name: 'Mike',
location: 'New York',
},
];
const unique = arr.filter(
(obj, index) =>
arr.findIndex((item) => item.location === obj.location) === index
);
/*
[
{ name: 'John', location: 'Los Angeles' },
{ name: 'Kate', location: 'New York' }
]
*/
console.log(unique);
const arr = [
{
name: 'Kate',
location: 'New York'
},
{
name: 'Mike',
location: 'New York'
},
{
name: 'Kate',
location: 'New York'
}
];
const unique = arr.filter(
(obj, index) =>
arr.findIndex(
(item) => item.location === obj.location && item.name === obj.name
) === index
)
/*
[
{ name: 'Kate', location: 'New York' },
{ name: 'Mike', location: 'New York' }
]
*/
console.log(unique);
const arr = [
{
name: 'John',
location: 'Los Angeles',
},
{
name: 'Kate',
location: 'New York',
},
{
name: 'Mike',
location: 'New York',
},
];
const unique = [];
for (const item of arr) {
const isDuplicate = unique.find((obj) => obj.location === item.location);
if (!isDuplicate) {
unique.push(item);
}
}
/*
[
{ name: 'John', location: 'Los Angeles' },
{ name: 'Kate', location: 'New York' }
]
*/
console.log(unique);
4、将两个数组合并为一个数组对象
// 第一种
for (let i = 0; i < dat.modelsType.length; i++) {
let str = {};
str.name = dat.modelsType[i]
str.value = dat.modelsCount[i]
this.chartend.push(str)
// console.log(this.chartend)
}
// 第二种
let old = dat.modelsType.map((name, i) => ({name, value: dat.modelsCount[i] }))
var a = ['DPD', 'DHL']
var b = [97400, 97402]
var c = a.map((name, i) => ({ name, value: b[i] }))
console.log(c);
5、数组问题-添加小数点
let arr = [25, 11.2315, '', 48.1, 86.01]
for (let i = 0; i < arr.length; i++) {
// 获取小数点的位置
let y = String(arr[i]).indexOf(".") + 1;
// 获取小数点后的个数
let count = String(arr[i]).length - y;
if (y > 0) {
if (count === 1) {
arr[i] += "0"
}
} else if (y === 0 && count !== 0) {
arr[i] += ".00"
}
}
console.log(arr)
let fund = [
{htl: 650, gtl: 291}
]
let fouce =parseFloat(fund[0].gtl / fund[0].htl).toFixed(2) * 100 + "%"
console.log(fouce)
5、js只保留数组对象的某个属性
let data = [
{ id: 1, name: 'pyq' },
{ id: 2, name: 'zs' }
]
let newData = []
// 第一种办法
data.map(i => {
newData.push(Object.assign({}, { name: i.name, }))
})
console.log(newData)
// 第二种办法
data.map((item,index) => {
newData.push({
value: item.name
})
})
console.log(newData)
6、取出两个数组中相同的值
let tableData = [
{
id: 1,
name: "张三",
},
{
id: 2,
name: "李四",
},
{
id: 3,
name: "王五",
}
]
let box = ["1"]
let arr = [];
arr = tableData.filter((item) =>
boxNo.some((i) => item.id== i)
);
console.log(arr)
let obj
let arr = []
arr = this.hisData.filter(item => res.data.bs.some(i => item == i))
this.hisSetData = arr
7、数组倒序排列
var array=['我','喜','欢','你'];
array.reverse(); // 输出: ["你", "欢", "喜", "我"]
8、字符串倒序排列
var string="Hello World"
var reverse=string.split("").reverse().join(""); //split()将字符串按特定的方式分割重组为一个数组 reverse()用于颠倒数组中元素的顺序join() 将数组按特定的方式重组为一个字符串
console.log(reverse); // 输出:dlroW olleH
9、将对象转换为数组对象
let str = { id: 1, name: '欣欣', sex: '女'}
let arr = [{...str}]
console.log(arr); // [{ id: 1, name: '欣欣', sex: '女'}]