使用jquery重构JSON输出

时间:2014-08-07 19:06:52

标签: jquery json

我通过json获得一个数组

[
    {"firstName":"John", "lastName":"Doe", "age":"34"}, 
    {"firstName":"Anna", "lastName":"Smith", "age":"28"}, 
    {"firstName":"Peter", "lastName":"Jones", "age":"36"}
]

但我希望结果如下

[
    {"firstName":"John", "age":"34"}, 
    {"firstName":"Anna", "age":"28"}, 
    {"firstName":"Peter", "age":"26"}
]

请使用jquery

帮助我解决方案

2 个答案:

答案 0 :(得分:1)

var people = [
    {"firstName":"John", "lastName":"Doe", "age":"34"}, 
    {"firstName":"Anna", "lastName":"Smith", "age":"28"}, 
    {"firstName":"Peter", "lastName":"Jones", "age":"36"}
];

$.each(people, function() {
   delete this.lastName;
});

答案 1 :(得分:0)

我认为你要完成的是将数组中的每个项目转换为新类型。使用jQuery,您可以使用.map()函数执行此操作。

http://api.jquery.com/jquery.map/

var originalArray = [
    {"firstName":"John", "lastName":"Doe", "age":"34"}, 
    {"firstName":"Anna", "lastName":"Smith", "age":"28"}, 
    {"firstName":"Peter", "lastName":"Jones", "age":"36"}
];

var newArray = $.map(originalArray, function(item) {
    return {
        "firstName": item.firstName,
        "age": item.age
    }
});