0

I'm working with a JSON feed that includes creative IDs in a field called 'creatives'. Some of these are individual IDs and some are multiple IDs.

For e.g. the JSON might look like:

0: {is_associated_creatives_in_adgroups: true,…}
creatives: ["1111", "2222", "3333", "4444",…]
16: {is_associated_creatives_in_adgroups: true, creatives: ["7777"],…}
creatives: ["7777"]

I'm running the following code:

adsAssociated = resp.data.data.filter(x=>x.is_associated_creatives_in_adgroups).map(y=>y.creatives);

The problem I have is that since some elements of 'creatives' are arrays with multiple values, I can't treat all the nested elements within this call as individual elements within the parent array ('creatives') itself. I tried to use:

adsAssociated = resp.data.data.filter(x=>x.is_associated_creatives_in_adgroups).map(y=>y.creatives.flat());

But this just maps the elements within the nested array as a comma-separated list mapped to the single element.

How do I get instead the expected output to map one-to-one, even the elements that are an array of multiple IDs? In other words, how do I get the JS to process it as if it were...

creatives: ["1111"]
creatives: ["2222"]
creatives: ["3333"]
creatives: ["4444"]
creatives: ["7777"]
2
  • Could you elaborate on the required output? Would this be required: [ [ '1111' ], [ '2222' ], [ '3333' ], [ '4444' ] , [ '7777' ] ] OR [ '1111', '2222', '3333', '4444', '7777' ] Commented Aug 15, 2020 at 15:56
  • The latter ... [ '1111', '2222', '3333', '4444', '7777' ] Commented Aug 15, 2020 at 16:02

1 Answer 1

2

Change this

adsAssociated = resp.data.data.filter(x=>x.is_associated_creatives_in_adgroups).map(y=>y.creatives.flat());

to this

adsAssociated = resp.data.data.filter(x=>x.is_associated_creatives_in_adgroups).map(y=>y.creative).flat();

The adsAssociated array needs to be flattened instead of the individual arrays.

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

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.