0

So this might look like a weird question but bear with me please:

I have a simple array of strings and I want to map it to an array of objects. pretty simple: I would write

arr.map (x => ({
   header  : x,
   sortType: 'basic'
}))

now, here is the question: I would like to check and see if x has a certain value then do not include the sortType at all. I would like to do something like

 arr.map (x => ({
       header  : x,
       x==='test' ? (sortType: 'basic') : //don't provide anything
    }))

so I would like my final array be something like this: And I do not want to have two maps ofcourse!

[
{ header: 'Header One' , sortType: 'basic'},
{ header: 'test' },
{ header: 'Another one' , sortType: 'basic'},
]
1
  • did you have a chance to check out my solution? It seems to be both concise and close to your initial attempt. If you faced issues, deploying that, I'd gladly support you, otherwise you may upvote and accept the answer to promote the solution and mark your question answered. Commented Mar 12, 2020 at 16:30

5 Answers 5

3

You could use two objects with a conditional operator.

arr.map(header => header === 'test'
    ? { header }
    : { header, sortType: 'basic' }
)

Or take Object.assign

arr.map(header => Object.assign({ header }, header !== 'test' && { sortType: 'basic' })) 
Sign up to request clarification or add additional context in comments.

3 Comments

or header => Object.assign({ header }, header === 'test' ? { sortType : 'basic' }, {} ) (possibly more expensive, but arguably more flexible and avoids repetition)
Object.assign omits false, a simple && works.
Even better (and the syntax on my first one isn't quite right somewhere TBD)
3

Spread short-circuit evaluation result


You may use spreading (...) of expression that conditionally evaluates into necessary sub-object ({sortType: 'basic'}):

const src = ['Header One','test','Another one'],
      result = src.map (header => ({
        header, 
        ...(header!=='test' && {sortType: 'basic'})
      }))
      
console.log(result)
.as-console-wrapper{min-height:100%;}

Comments

1

I know you got quite some nice answers already, in case you prefer this approach.

const arr = ['penguinsinpijamas', 'test'];

var mappy = arr.map(x => {
    let obj = {'header': x};
    if (obj.header === 'test') obj.sortType = 'basic';
    return obj})

mappy.forEach(m => console.log(m))

Returns:

Object {header: "penguinsinpijamas"}
Object {header: "test", sortType: "basic"}

1 Comment

The OP said their input was a plain array of strings.
1
 var arr = ['test','love','javascript'];

 const output = arr.map (x => {
       const obj = {
           header  : x
       };
       if(x === "test") obj.sortType = "basic";
       return obj;
});

console.log(output);
// [{header: "test", sortType: "basic"}, {header: "love"}, {header: "javascript"}]

1 Comment

Please edit your answer with a textual explanation of the code for future readers to follow along.
-3

Try this

var arr = ['test','love','javascript'];
    var result = arr.map (x => ({
           header  : x,
           sortType: x==='test'?undefined:'basic'
        }))
    console.log(JSON.stringify(arr));
    console.log(JSON.stringify(result));

output :

["test","love","javascript"]
[{"header":"test"},{"header":"love","sortType":"basic"},{"header":"javascript","sortType":"basic"}]

you should apply logic for the value.

2 Comments

no. I don't want to include the sortType, not to include it with a ''!!
The result and answer are not correct. It always includes the key sortType (albeit sometimes with an undefined value)

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.