数组的操作。1,JS数组去重。2,把数组中存在的某个值,全部找出来。3在JS数组指定位置插入元素。。。

1,数组去重

let arr = [1,2,3,4,5,6,1,2,3,'a','b','a'];
let temp = []; // 作为存储新数组使用
for(let i = 0; i < arr.length; i++){
   if(temp.indexOf(arr[i]) == -1){
      temp.push(arr[i]);
   }
}
console.log(temp) //  [1, 2, 3, 4, 5, 6, "a", "b"]

2,把数组中存在的某个值,全部找出来

let list = [
  {name:'你的名字',age:18},
  {name:'你的名字1',age:18},
  {name:'你的名字2',age:18},
  {name:'你的名字3',age:18},
  {name:'你的名字4',age:18},
  {name:'你的名字5',age:18},
];
let arr = [];
list.forEach(item => {
  arr.push(item.name);
});
console.log("新数组:",arr) //..["你的名字", "你的名字1", "你的名字2", "你的名字3", "你的名字4", "你的名字5"]

3,在JS数组指定位置插入元素

let array = ["one", "two", "four"];
array.splice(2, 0, "three");  // 拼接函数(索引位置, 要删除元素的数量(0为不删除), 新增的元素)
console.log(array);  //  ["one", "two", "three", "four"]

4,JS判断数组中是否存在某一数值的方法

a、常规JS方法

var arr = ['qqq','www','rrrr']; 
console.log(arr.indexOf('qqq')) //如果存在返回值的下标,不存在返回-1

b、采用ES7方式

let arr = ['猪', '兔子', '大熊猫'];
if (arr.includes('猪')){
  console.log('猪存在');  //..存在就返回 true,否则false
}

5,数组里面存在某个键和值,则返回对应的对象(适用vue)

let nulldataL = [
   {clmuCode : 'SRSCNZJLD',txt : '文本1'},
   {clmuCode : 'QRWSFFDF',txt : '文本2'},
];
let newArr = nulldataL.filter(item => item.clmuCode === "SRSCNZJLD")[0];
console.log(newArr) //  {clmuCode: "SRSCNZJLD", txt: "文本1"}