日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 获取 js json数据类型,JS基础-JS的数据类型和访问/流程控制/JSON格式字符串和js对象相互转换...

發布時間:2024/9/27 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 获取 js json数据类型,JS基础-JS的数据类型和访问/流程控制/JSON格式字符串和js对象相互转换... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JS的數據類型和訪問/流程控制/JSON格式字符串和js對象相互轉換

1. JS的數據類型和訪問

1.1. 原始類型JS中的原始數據類型有: number , string , boolean ;

聲明變量使用 var 關鍵字.

/* 數字類型 */

varyear=2020;

/* 字符串類型 */

varname='zhangsan';

/* 布爾類型 */

varisUpdate=false;

獲取變量的數據類型使用 typeof . 使用方式: typeof 變量名 .

varsite='php.cn';

// 使用typeof來獲取變量的數據類型

vardataType=typeofsite;

document.write(dataType);

/* result: string */

1.2 特殊類型JS中兩個特殊的值類型: null , undefined .

當只聲明變量, 沒有初始化變量值時, 此時變量的值是 undefined . 即, var param; 等效于 var param = undefined; .

varparam;

document.write(param);

/* result: undefined */

varparam1=null;

document.write(param1);

/* result: null */

特別的: null == undefined 返回的是 true ; 而 null === undefined 則返回 false

document.write(null==undefined);

/* result: true */

document.write(null===undefined);

/* result: false */

null 和 undefined 的區別: null 表示空對象; undefined 表示非對象類型的變量值為空/無.

// 獲取null的數據類型

vardataType=typeofnull;

document.write(dataType);

/* result: object */

null / undefined 轉換為原始類型數據的值

null / undefined 轉換為布爾值, 值轉為為 false ;

if(!null)document.write('我被輸出了
');

if(!undefined)document.write('我被打印了');

/* result:

我被輸出了

我被打印了 */

null / undefined 轉換為字符串時, 值轉為: "null" / "undefined" .

document.write(null+'
');

document.write(undefined+'
');

/* result:

null

undefined */

null 轉為數值時, 值為: 0 ; undefined 則不能轉為數字(輸出: NaN).

document.write(null+100);

document.write(undefined+100);

/* result:

100

NaN

*/

1.3 對象類型: array , object , function在js中, 除開 null 和 undefined , 所有數據類型都是對象, 包括原始類型.

// 證明原始類型數據也是對象: 都能通過他們調用內置方法

varnum=3.1415926;

varname='zhangsan';

// 把num的值四舍五入, 保留3位小數

console.log(num.toFixed(3));

console.log(name.toUpperCase());

/* result:

3.142

ZHANGSAN

*/

1.3.1 數組類型JS中的數組跟PHP中的索引數組類似

// 定義數組

varplayer=['James','Davis','Green','Rondo','Kuzma'];

document.writeln(player);

/* result: James,Davis,Green,Rondo,Kuzma James */

// 獲取數組的中的元素

document.writeln(player[0]);

/* result: James */

判斷變量是否為數組類型, 用 Array.isArray(變量名) , 因為 typeof 變量名 的返回值是 object , 不夠具體.

// 定義數組

varplayer=['James','Davis','Green','Rondo','Kuzma'];

document.writeln('"typeof player" 的值是:'+(typeofplayer));

document.writeln('變量"player"的值'+(Array.isArray(player)?'是':'不是')+'數組');

/* result:

"typeof player" 的值是:object

變量"player"的值是數組

*/

數組循環

for 循環

varplayer=['James','Davis','Green','Rondo','Kuzma'];

for(varindex=0;index

document.write(player[index]+", ");

}

/* result: James, Davis, Green, Rondo, Kuzma, */

forEach 循環

使用格式: array.forEach(function(當前遍歷到的元素, 當前遍歷到的元素在數組中的索引[可選], 受遍歷的數組[可選]) {...})

varplayer=['James','Davis','Green','Rondo','Kuzma'];

player.forEach(function(p,index){

document.write("第"+(index+1)+"位出場的是"+p+'
');

});

/*

第1位出場的是James

第2位出場的是Davis

第3位出場的是Green

第4位出場的是Rondo

第5位出場的是Kuzma

*/

數組函數

array.slice 函數

用法: targetArr.slice(起始位置, 結束位置 + 1) : 獲取JS數組中的”起始位置”到”結束為止”之間的元素

varplayer=['James','Davis','Green','Rondo','Kuzma'];

// 返回前3個球員

document.write(player.slice(0,3));

/* result : James,Davis,Green */

array.splice 函數

類似PHP中的數組函數 splice , 可以用它實現對數組元素的增刪改.

使用語法: array.splice(起始位置, 元素個數[可選], 替換元素1[可選], 替換元素2[可選]...) ; 其中 替換元素1, 替換元素2... 可以以數組的形式傳入: [替換元素1, 替換元素2...]

實現向當前數組中插入元素: targetArr.splice(起始位置, 0, 插入元素1, 插入元素2...)

varplayer=['James','Davis','Green','Rondo','Kuzma'];

// 在Davis后面插入Bradley, McGee

player.splice(2,0,['Bradley','McGee']);

document.write(player);

/* result: James,Davis,Bradley,McGee,Green,Rondo,Kuzma */

-2.實現刪除當前數組中的元素:`targetArr.splice(起始位置, 元素個數)`

varplayer=['James','Davis','Green','Rondo','Kuzma'];

// 刪除Green, Rondo

player.splice(2,2);

document.write(player);

/* result: James,Davis,Kuzma */

-3.實現更新當前數組中元素的值:`targetArr.splice(起始位置, 元素個數, 替換后的值1, 替換后的值2[可選]...)`

varplayer=['James','Davis','Green','Rondo','Kuzma'];

// 把Davis, Green, Rondo更新為中文名

player.splice(1,3,['戴維斯','格林','隆多']);

document.write(player);

/* result: James,戴維斯,格林,隆多,Kuzma */

1.3.2 對象JS中的對象跟PHP中的關聯數組相似.

定義對象

// 創建對象-1

varplayer1={

name:'James',

team:'湖人',

age:35,

// JS中, 如果屬性名含有非法字符, 則用雙引號 `"` 將其括起來

"player info":{

height:'203',

weight:'113',

position:'F-G'

}

};

// 以表格的形式打印對象

console.table(player1);

// 另一種定義對象屬性的方式見下面的例子

執行結果:

訪問對象屬性, 有兩種方式一種是: 對象名.屬性名 ; 另一種是類似PHP中的關聯數組: 對象名[屬性名] . 還有非法字符的屬性名, 只能用第二種方式訪問.

// 創建對象-2

varplayer2={};

player2.name="James";

player2.team='湖人';

player2.age=35;

player2["player info"]={

height:'203',

weight:'113',

position:'F-G'

};

// 訪問對象屬性

document.write('姓名:'+player2.name+'
');

document.write('球隊:'+player2.team+'
');

document.write('年齡:'+player2['age']+'
');

document.write('身高:'+player2['player info'].height+'
');

document.write('體重:'+player2['player info']['weight']+'
');

document.write('司職:'+player2['player info']['position']+'
');

/*

姓名:James

球隊:湖人

年齡:35

身高:203

體重:113

司職:F-G

*/

遍歷對象屬性

遍歷對象方法1: 使用 for 屬性名 in 對象 . 語法: for(對象鍵名 in 對象), 在遍歷中, 用 對象名["屬性名"] 的方式獲取元素值更穩一些, 避免出現非法字符遍歷報錯.

varplayer1={

name:'James',

team:'湖人',

age:35,

// JS中, 如果屬性名含有非法字符, 則用雙引號 `"` 將其括起來

"player info":{

height:'203',

weight:'113',

position:'F-G'

}

};

// for...in...遍歷對象屬性.

for(prop in player1){

varvalue=player1[prop];

if(typeofvalue!='object')

document.write(prop+': '+value+'
');

else// 如果屬性值是對象, 繼續遍歷

for(prop1 in value){

varvalue1=value[prop1];

document.write(prop1+': '+value1+'
');

}

}

/* result

name: James

team: 湖人

age: 35

height: 203

weight: 113

position: F-G

*/

遍歷對象方法2: 使用 Object.keys(對象) 獲取對象的屬性名組成的數組, 再借助數組的 forEach 進行遍歷.

varplayer1={

name:'James',

team:'湖人',

age:35,

// JS中, 如果屬性名含有非法字符, 則用雙引號 `"` 將其括起來

"player info":{

height:'203',

weight:'113',

position:'F-G'

}

};

// 獲取屬性名數組

varprops=Object.keys(player1);

props.forEach(function(prop){

varvalue=player1[prop];

if(typeofvalue!='object')

document.write(prop+': '+value+'
');

else{

// 如果屬性值是對象, 繼續遍歷

varprops1=Object.keys(value);

props1.forEach(function(prop1){

varvalue1=value[prop1];

document.write(prop1+': '+value1+'
');

});

}

});

/* result:

name: James

team: 湖人

age: 35

height: 203

weight: 113

position: F-G

*/

總結

以上是生活随笔為你收集整理的php 获取 js json数据类型,JS基础-JS的数据类型和访问/流程控制/JSON格式字符串和js对象相互转换...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。