Home > Web Front-end > JS Tutorial > body text

json数据处理技巧(字段带空格、增加字段、排序等等)_javascript技巧

WBOY
Release: 2016-05-16 17:32:18
Original
1521 people have browsed it

1、json数据的正常取值:json[i].fieldName

2、json数据的字段带空格:eval('json[' + i + ']["' + field + '"]')

3、json数据的赋值:eval('json[' + i + ']["' + field + '"]=' + jsonFilter.length);

4、json数据增加字段:循环所有数据,直接json[i].newField=defaultValue就可以了

5、json数据的排序:相当于数组的排序,json.sort(function (a, b) { return a[name] > b[name] ? 1 : -1 });

6、json数据的拷贝:var newObject = jQuery.extend(true, {}, oldObject); 请参考:jQuery中json对象的复制(数组及对象)

7、json数组添加和删除等:concat 方法 | join 方法 | pop 方法 | push 方法 | reverse 方法 | shift 方法 | slice 方法 | sort 方法 | splice 方法 | toLocaleString 方法 | toString 方法 | unshift 方法 | valueOf 方法

8、创建json数组的例子:var jsonArr=[]; var item={id:'id1',value:'val1'}; jsonArr.push(item); 这样就创建了有两个字段id和value的json数组,并且给数组中增加了一条记录。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!