Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // INPUT
- let people = [
- {
- "firstname": "John",
- "lastname": "Doe",
- "age": 20,
- "address": {
- "city": "San Francisco",
- "state": "CA"
- }
- },
- {
- "firstname": "Bob",
- "lastname": "Lee",
- "age": 30,
- "address": {
- "city": "New York",
- "state": "NY"
- }
- },
- ];
- // COMBINE FIRST AND LAST NAME INTO NEW JSON PROPERTY
- results = people.map(function (currentValue, index, arr) {
- // add a property to each object
- currentValue.fullname = currentValue.firstname + " " + currentValue.lastname;
- return currentValue;
- });
- // OUTPUT
- /*[
- {
- "firstname": "John",
- "lastname": "Doe",
- "age": 20,
- "address": {
- "city": "San Francisco",
- "state": "CA"
- },
- "fullname": "John Doe"
- },
- {
- "firstname": "Bob",
- "lastname": "Lee",
- "age": 30,
- "address": {
- "city": "New York",
- "state": "NY"
- },
- "fullname": "Bob Lee"
- },
- ]*/
Add Comment
Please, Sign In to add comment