js jq 字符串数组对象

数组是有序的,对象是无序,数组是特殊的对象

数组

声明数组
var arr=new Array('red','blue','yellow');  //["red", "blue", "yellow"]
var arr=["red", "blue", "yellow"];
var arr=new Array();
     arr[0]='red';
     arr[1]='blue';
     arr[2]='yellow';

追加一个元素
arr.push('green');   //["red", "blue", "yellow","green"]

获取数组的元素个数
var len=arr.length;

length的长度是根据索引得来

arr['name']='xiaozhu';
arr.length   //4

arr[8]='xiaozhu';
arr.length   //9

对象

声明对象
var person = new Object();
    person.name = "xiaozhu";
    person.age = 21;
   
    var person = {
        name: "xiaozhu",
        age : 21
    }

//{name: "xiaozhu", age: 21}

 

字符串 数组 数组格式化(对象)

字符串转数组
var str="a,s,d";
var arr = str.split(',');         // ["a","s","d"]  数组

数组转数组格式化(字符串)
str = JSON.stringify(arr);        // ["a","s","d"]  字符串

数组格式化(字符串)转数组
arr = JSON.parse(str );          // ["a","s","d"]  数组

 
JSON字符串转换为JSON对象
var str = '{ "name": "cxh", "sex": "man" }';
var obj = eval('(' + str + ')');    //{name: "cxh", sex: "man"}   对象
var obj =jQuery.parseJSON();
var obj = JSON.parse(str);

JSON对象转换为JSON字符串

var str = { "name": "cxh", "sex": "man" };
str = JSON.stringify(str ); //{name: "cxh", sex: "man"}   字符串

数组转字符串
var arr=["a","s","d"];
var str =arr.join(',');         // a,s,d     字符串
var str=arr.toString();         //a,s,d 字符串

循环遍历

 遍历数组
 var arr=["a","b","c"];
 $.each(arr,function(i,item){
        console.log(i+"--"+item);
 });

 遍历对象
  var obj={name:"xiaozhu",age:20};
  $.each(obj,function(i,item){
        console.log(i+"--"+item);
 });

 

posted @ 2019-04-25 16:25  小猪哥哥  阅读(7130)  评论(0编辑  收藏  举报