标签:style io color ar java for sp on 问题
首先感谢Dash 我再也不用到处乱找文档了
再次感谢日食记 让我的看到了世界的美好
好吧 array有什么好玩的方法吗 splice 很好玩的
splice 可以对数组进行删除 添加 修改操作
var myFish = [‘angel‘, ‘clown‘, ‘mandarin‘, ‘surgeon‘]; // 插入 // 找到下表为2的位置 删除0个元素 插入 ‘drum‘ var removed = myFish.splice(2, 0, ‘drum‘); // myFish is [‘angel‘, ‘clown‘, ‘drum‘, ‘mandarin‘, ‘surgeon‘] // removed is [], no elements removed // 在数组3的位置删除一个元素 removed = myFish.splice(3, 1); // myFish is [‘angel‘, ‘clown‘, ‘drum‘, ‘surgeon‘] // removed is [‘mandarin‘] // 找到下表为2的位置 删除1个元素 插入 ‘drum‘ // 实际是执行了替换操作 removed = myFish.splice(2, 1, ‘trumpet‘); // myFish is [‘angel‘, ‘clown‘, ‘trumpet‘, ‘surgeon‘] // removed is [‘drum‘] // 在位置0删除2个 增加3个 removed = myFish.splice(0, 2, ‘parrot‘, ‘anemone‘, ‘blue‘); // myFish is [‘parrot‘, ‘anemone‘, ‘blue‘, ‘trumpet‘, ‘surgeon‘] // removed is [‘angel‘, ‘clown‘] // 从某一位置删除后面所有元素 removed = myFish.splice(3, Number.MAX_VALUE); // myFish is [‘parrot‘, ‘anemone‘, ‘blue‘] // removed is [‘trumpet‘, ‘surgeon‘]
sort方法
array的sort方法一点也不准确
var fruit = [‘apples‘, ‘bananas‘, ‘Cherries‘]; fruit.sort(); // [‘Cherries‘, ‘apples‘, ‘bananas‘]; var scores = [1, 2, 10, 21]; scores.sort(); // [1, 10, 2, 21] var things = [‘word‘, ‘Word‘, ‘1 Word‘, ‘2 Words‘]; things.sort(); // [‘1 Word‘, ‘2 Words‘, ‘Word‘, ‘word‘] // In Unicode, numbers come before upper case letters, which come before lower case letters.
实际上很准确啦 说是根据unicode码排序的
so 你可以自己写规则
function compare(a, b) { if (a < b) { return -1; } if (a > b) { return 1; } // a must be equal to b return 0; }
比如数字排序
var numbers = [-4, -2, 5, 1, 3]; console.log(numbers.sort());
是不准确的
var numbers = [-4, -2, 5, 1, 3]; numbers.sort(function compare(a, b) { if (a < b) { return -1; } if (a > b) { return 1; } // a must be equal to b return 0; }); console.log(numbers);
解决非Ascii码字符串排序
var items = [‘réservé‘, ‘premier‘, ‘cliché‘, ‘communiqué‘, ‘café‘, ‘adieu‘]; items.sort(function (a, b) { return a.localeCompare(b); });
concat 扩展数组 没啥好说的
join 把数组变成字符串
slice 复制指定位置 形成新的array
var fruits = [‘Banana‘, ‘Orange‘, ‘Lemon‘, ‘Apple‘, ‘Mango‘]; var citrus = fruits.slice(1, 3); // citrus contains [‘Orange‘,‘Lemon‘]
有关复制后形成的新数组变量问题
Using slice, create newCar from myCar. var myHonda = { color: ‘red‘, wheels: 4, engine: { cylinders: 4, size: 2.2 } }; var myCar = [myHonda, 2, ‘cherry condition‘, ‘purchased 1997‘]; var newCar = myCar.slice(0, 3); console.log(myCar); console.log(newCar); // Change the color of myHonda. myHonda.color = ‘purple‘; console.log(‘The new color of my Honda is ‘ + myHonda.color); // Display the color of myHonda referenced from both arrays. console.log(‘myCar[0].color = ‘ + myCar[0].color); console.log(‘newCar[0].color = ‘ + newCar[0].color); var myCar = []; myCar = [{color:‘red‘, wheels:4, engine:{cylinders:4, size:2.2}}, 2, ‘cherry condition‘, ‘purchased 1997‘]; var newCar = [{color:‘red‘, wheels:4, engine:{cylinders:4, size:2.2}}, 2]; myCar[0].color = ‘purple‘; console.log(myCar); console.log(newCar);
从上面的例子看出并非完全不一样 里面有Object的话还是会一起改
标签:style io color ar java for sp on 问题
原文地址:http://my.oschina.net/u/1421356/blog/340000