0

I get an array by json

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

but am expecting the result as below

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

Please help me with a solution using jquery

2 Answers 2

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;
});
Sign up to request clarification or add additional context in comments.

Comments

0

I think what you're trying to accomplish is transform each item in the array to a new type. With jQuery, you can do this with the .map() function.

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
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.