---
title: "JS 访问对象中数据易错点总结"
author: "wsj123"
date: "2016-04-16T16:07:18Z"
tags: ["JavaScript"]
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2016-04-16T16:07:18Z"
id: 2291780
url: "https://wsj123.iteye.com/blog/2291780"
---

JS 访问对象中数据易错点总结
1.1访问Hash中的键值
例1：
var age = {'Tom':18,'Jack':19};
查询哈希表中的'Jack'的数据：
var data = age;
var data=age. 'Jack'<span style="color:blue">//错误的方式。当属性为字符串时，只能采用的方式</span>
例2：
var stu_info = {name:'王雪',age:20,num:123,math_score:136};
查询哈希表中的math_score的数据：
var num_math_score=stu_info.math_score;
var num_math_score=stu_info;
var num_math_score =stu_info;<span style="color:blue">//错误方式</span>
<span style="color:blue">当属性为变量时，两种方式都可以采用，但采用方式时必须给变量名加引号。</span>
1.2将对象转化为另一个Hash中某一个属性。
var inputs=;
var barcode_count={};
统计inputs数组中重复元素的个数并存为键值对：
forEach(inputs,function(barcode){
if(!barcode_count){
	  barcode_count=1;
<span style="color:blue">//将字符串'ITEM000001'转化为barcode_count 哈希中的一个属性ITEM000001。</span>
}else{
barcode_count++;
}
})