0

Right now I have data like the below:

[ 
        { 
           "integration_id":7,
           "campaign_id":4,
           "integration_name":"unbounce",
           "url_string":"23432fsaf",
           "created_at":"2019-12-23 21:05:45",
           "updated_at":"2019-12-23 21:05:45"
        },
        { 
           "integration_id":37,
           "campaign_id":4,
           "integration_name":"clickfunnels",
           "url_string":"yttzbe9rwd",
           "created_at":"2019-12-27 19:19:02",
           "updated_at":"2019-12-27 19:19:02"
        },
        { 
           "integration_id":47,
           "campaign_id":4,
           "integration_name":"instapage",
           "url_string":"vaqcjq8iy2",
           "created_at":"2019-12-29 16:52:01",
           "updated_at":"2019-12-29 16:52:01"
        }
]

But I want to transform this array into an array like below:

[
        { 
           "integration_name":"unbounce",
        },
        { 
           "integration_name":"clickfunnels",          
        },
        {  
           "integration_name":"instapage",            
        }
 ]

I want to keep the integration names but remove all other properties from the items. How can I achieve this?

4
  • what did you try? Show your code. Commented Dec 29, 2019 at 21:15
  • Sounds like a job for Array.prototype.map Commented Dec 29, 2019 at 21:16
  • array.map(({integration_name}) => ({integration_name})); Commented Dec 29, 2019 at 21:17
  • 3
    Please don't destroy the edits that people make to your question to make the code render well. Commented Dec 29, 2019 at 21:18

2 Answers 2

3

You can simply .map() over your data, and return only the integration_name Key / Value using destructuring assignment

const data = [{
    "integration_id": 7,
    "campaign_id": 4,
    "integration_name": "unbounce",
    "url_string": "23432fsaf",
    "created_at": "2019-12-23 21:05:45",
    "updated_at": "2019-12-23 21:05:45"
  },
  {
    "integration_id": 37,
    "campaign_id": 4,
    "integration_name": "clickfunnels",
    "url_string": "yttzbe9rwd",
    "created_at": "2019-12-27 19:19:02",
    "updated_at": "2019-12-27 19:19:02"
  },
  {
    "integration_id": 47,
    "campaign_id": 4,
    "integration_name": "instapage",
    "url_string": "vaqcjq8iy2",
    "created_at": "2019-12-29 16:52:01",
    "updated_at": "2019-12-29 16:52:01"
  }
];

let newArray = data.map(({integration_name}) => ({integration_name}));

console.log(newArray);

Sign up to request clarification or add additional context in comments.

2 Comments

You can change the lambda to this: => ({integration_id})
Thank you; also made me realise I had wrote the wrong key initially
1

You can use Array.map with Object destructuring

let input = [{ "integration_id":7, "campaign_id":4, "integration_name":"unbounce", "url_string":"23432fsaf", "created_at":"2019-12-23 21:05:45", "updated_at":"2019-12-23 21:05:45" }, { "integration_id":37, "campaign_id":4, "integration_name":"clickfunnels", "url_string":"yttzbe9rwd", "created_at":"2019-12-27 19:19:02", "updated_at":"2019-12-27 19:19:02" }, { "integration_id":47, "campaign_id":4, "integration_name":"instapage", "url_string":"vaqcjq8iy2", "created_at":"2019-12-29 16:52:01", "updated_at":"2019-12-29 16:52:01" }]

let result = input.map(({integration_name}) => ({integration_name}));

console.log(result);

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.