JavaScripts 數組 Json相關

數組
 1、數組的長度可變、不會產生數組越界問題
 2、同一數組中的元素類型可以不同
 3、訪問未賦值的數組元素時,該元素的值爲undefind

json 相關操作

json --> 數組

let jsonObj = {'未完成':5, '已完成':8, '待確認':4, '已取消':6};
let arr = []
for (let i in jsonObj) {
    let item = {};
    item[i] = jsonObj[i];
    arr.push(item);
}
console.log(arr); // [{未完成: 5},{已完成: 8},{待確認: 4},{已取消: 6}]

獲取json對象長度

let jsonlen=0;
for(let key in brokers){ 
  jsonlen ++;
}

數組相關操作

splice() : 刪除數組元素,並返回刪除後的數組

var arr1 = ["小明","小剛","小白","小熊"];
var arr2 = [];
arr2 = arr1.splice(2,1);
console.log(arr1); //  ["小明", "小剛", "小熊"]
console.log(arr2); //  ["小白"]

filter:對數組元素進行過濾,返回數組

語法:array.filer(function(value, index, array) {// ...});

(1) 篩選排除所有較小的值

var data = [12, 2, 8, 44,130];
var arrayOfSquares = data.filter(item =>{
    return item > 10;
});
console.log(arrayOfSquares); // 12,44,130

(2) 根據條件過濾數組內容

var users = [
    {name: "張一", "email": "[email protected]"},
    {name: "江二",   "email": "[email protected]"},
    {name: "李三",  "email": "[email protected]"},
    {name: "王二",  "email": "[email protected]"}
];
var userArr = users.filter(item =>{return item.name.indexOf('二')>-1});
console.log(userArr); // [{name: "江二",   "email": "[email protected]"},{name: "王二",  "email": "[email protected]"}];

map:對數組元素進行處理,返回數組

語法:array.map(function(value, index, array) {// ...});

(1) 原數組被“映射”成對應新數組

var data = [1, 2, 3, 4];
var arrayOfSquares = data.map(function (item) {
    return item * item;
});
console.log(arrayOfSquares); // 1, 4, 9, 16

(2) 獲得數組中的特定屬性值

var users = [
    {name: "張一", "email": "[email protected]"},
    {name: "江二",   "email": "[email protected]"},
    {name: "李三",  "email": "[email protected]"}
];
var emails = users.map(function (user) { return user.email; });
console.log(emails.join(", ")); // [email protected], [email protected], [email protected] 

(3)向數組新增屬性

 var users = [
    {name: "張一", "email": "[email protected]"},
    {name: "江二",   "email": "[email protected]"},
    {name: "李三",  "email": "[email protected]"}
 ];
 users  = users.map(function (user,index) { 
       user.index = index;
       return user; 
 });
 console.log(JSON.stringify(users)); // [{"name":"張一","email":"[email protected]","index":0},{"name":"江二","email":"[email protected]","index":1},{"name":"李三","email":"[email protected]","index":2}] 

sort: 對數組元素進行排序,返回數組

語法: arr.sort(compareFunction)
          compareFunction 可選,如果省略,元素根據字符串Unicode碼點進行默認排序
          compareFunction(a, b) 小於 0 ,那麼 a 會被排列到 b 之前
          compareFunction(a, b) 等於 0 , a 和 b 的相對位置不變(不保證所有瀏覽器順序一定這樣)
          compareFunction(a, b) 大於 0 , b 會被排列到 a 之前

碼點參考:http://www.ruanyifeng.com/blog/2014/12/unicode.html

(1) 比較數字

var number = [21,37,45,-12,0,37];
// number.sort((a,b) => a - b); //升序排列
number.sort((a,b) => b - a); //降序排列
console.log(number); //[45, 37, 37, 21, 0, -12]

(2) 比較對象

 var items = [
       { name: 'Edward', value: 21 },
       { name: 'Sharpe', value: 37 },
       { name: 'And', value: 45 },
       { name: 'The', value: -12 }, 
       { name: 'Magnetic',value: 0 },
       { name: 'Zeros', value: 37 } 
];
items.sort( (a, b) => a.value - b.value);
console.log(items); //[{name: "The", value: -12},{name: "Magnetic", value: 0},{name: "Edward", value: 21},{name: "Sharpe", value: 37},{name: "Zeros", value: 37},{name: "And", value: 45}]

參考:關於ES3、ES5、ES6以及ES7所有數組的方法(api)的總結

判斷數組中是否存在某個對象

(1) 數組元素是基本類型

var arr = [1,2,3,4];
arr.indexOf(3); // 2

(2) 數組元素是對象

  var arr = [{'name': 'tom'},{'name': 'jerry'},{'name':'jack'}];
  arr.map(item =>{
     if(item.name == 'jack'){
       console.log('存在該元素!');
     }
  });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章