0

I am trying to get an array of objects based on a condition. If the value is true then only add that key and value to new array of object.

But, It gives error:

const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]

const newArr = arr.map(item => ({id: item.id, val: item.val != null ? item.val : ''}))

console.log(newArr); //It is working

const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]

const expectedArr = arr.map(item => ({id: item.id, val: item.val != null ? item.val : '', (item.show) && (show: item.show)}))

console.log(expectedArr); //Shows error

Expected Result:

[
  {
    "id": 123,
    "val": "abcd"
  },
  {
    "id": 123,
    "val": "abcd"
    "show": true
  },
  {
    "id": 123,
    "val": "abcd"
  },
  {
    "id": 123,
    "val": "abcd"
  }
]

Any help would be greatly appreciated.

6
  • 1
    (show) && (show: val.show)}) should be (item.show) && (show: item.show)})? Commented Oct 16, 2020 at 12:42
  • @lissettdm Sorry, typo Commented Oct 16, 2020 at 12:44
  • Change (item.show) && (show: item.show) to ...(item.show && {show: item.show}) Commented Oct 16, 2020 at 12:48
  • @Yousaf No that one also not working Commented Oct 16, 2020 at 12:54
  • 1
    Working Demo Commented Oct 16, 2020 at 12:57

4 Answers 4

3

that ?

const arr = 
      [ { id: 123, val: 'abcd', other: 'abcd' } 
      , { id: 123, val: 'abcd', other: 'abcd', show: true  } 
      , { id: 123, val: 'abcd', other: 'abcd', show: false } 
      , { id: 123, val: 'abcd', other: 'abcd' } 
      ] 

const newArr = arr.map(({id,val,show})=>show?{id,val,show}:{id,val})

console.log( newArr )
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

2

You can rewrite it like this. with this syntax you, when item.show is true the parenthesis resolve to and object and then you just spread it into your object. You can also use nullish coalescing to assign item.val

 const expectedArr = arr.map((item) => ({
      id: item.id,
      val: item.val ?? "",
      ...(item.show && { show: item.show }),
    }));

Comments

1

Currently, it contains multiple conditions on one line inside Array.map function and it's not valid.

const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]

const result = arr.map(({ id, val, show }) => {
  const newObj = {
    id,
    val: val != null ? val : ''
  };
  if (show) {
    newObj['show'] = show;
  }
  return newObj;
});

console.log(result);

Comments

0

const arr = [{id: 123, val: 'abcd', other: 'abcd'}, {id: 123, val: 'abcd', other: 'abcd', show: true}, {id: 123, val: 'abcd', other: 'abcd', show: false}, {id: 123, val: 'abcd', other: 'abcd'}]

const expectedArr = arr.map(item => ({id: item.id, val: item.val? item.val: '', show: item.show? item.show: 0}))

console.log(expectedArr); //solved-ish

//but honestly, that's the furthest i can go with your strange code golfed syntax setup.. to be honest, using ? is similar to if but NOT if, it's closer to || but anyway

console.log('skip what was above, the moment of truth below')

const theRealAnswer = arr.map(item=>{ const i={id: item.id, val: item.val? item.val: '', show: item.show? item.show: 0};if(!i.show){delete(i.show)}return(i) })

console.log(theRealAnswer)

//now, the logical construct(what u gave the mapper) was impossible to do what u wanted. coding isn't just syntax, it's logic too. you cannot assign a key to completely delete the key itself

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.