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

R9102
  • 687
  • 1
  • 9
  • 32
  • What have you tried to achieve this? – mykaf Oct 13 '22 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 – R9102 Oct 13 '22 at 18:41
  • Please include your attempts and we can help debug specific questions or stumbling points. – mykaf Oct 13 '22 at 18:49

1 Answers1

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',
    },
  ]
R9102
  • 687
  • 1
  • 9
  • 32
Nekå
  • 26
  • 6