0

i have a nested array of objects in which I need to add condition and remove objects.

     const Items = [
    {
      A: 'title',
      B: [],
      C: [
        { item1: item1},
        { item2: item2 },
        { 'item3': item3},
        { 'item4': item4},
      ],
      D: 'some value',
    },
  ]

if my variable test is true i need below output else same as above

  const test  = true;// not always true
  then I need my output as  

    const Items = [
    {
      A: 'title',
      B: [],
      C: [
        { item1: item1},
        { item2: item2 },
      ],
      D: 'some value',
    },
  ]

{ 'item3': item3},{ 'item4': item4}, should be removed

3
  • What have you tried to achieve this? Commented Oct 13, 2022 at 18:36
  • @mykaf tried adding this twice using the ternary operator so want to optimize in better way i didn't add any tried one test?items: newitems Commented Oct 13, 2022 at 18:41
  • Please include your attempts and we can help debug specific questions or stumbling points. Commented Oct 13, 2022 at 18:49

1 Answer 1

1

You can use ternary for that :

const test  = true;

    const Items = [
    {
      A: 'title',
      B: [],
      C: test? [
        { item1: "item1"},
        { item2: "item2" },
      ] : [
    { item1: item1},
    { item2: item2 },
    { 'item3': item3},
    { 'item4': item4},
    ],
      D: 'some value',
    },
  ]
Sign up to request clarification or add additional context in comments.

1 Comment

i have changed your answer if you feel it is right can you approve

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.