0

Suppose I have an array of objects

const companyList = [
    {
        name: 'amazon',
        isIntermediary: false
    },
    {
        name: 'microsoft',
        isIntermediary: false
    },
    {
        name: 'talentsearch',
        isIntermediary: true
    },
    {
        name: 'talent global',
        isIntermediary: true
    },
    {
        name: 'taleo',
        isIntermediary: true
    }
];

I want to create 2 arrays. I can do so using reduce

const companies = companyList.reduce(
    (acc, curr) => {
        if (!curr.isIntermediary) {
            acc[0].push(curr);
        } else {
            acc[1].push(curr);
        }
        return acc;
    },
    [[], []]
);

Is there a way to refactor this code to use Ternary Operator instead and have it being a one-liner? I'm struggling to do so... Thanks for help !

1
  • what is the expected outpur?? Commented Aug 30, 2019 at 20:09

3 Answers 3

3

Boolean converted to Number becomes 0 or 1, and comma operator can be used to shorten it :

const companyList = [ { name: 'amazon',        isIntermediary: false },
                      { name: 'microsoft',     isIntermediary: false },
                      { name: 'talentsearch',  isIntermediary: true  },
                      { name: 'talent global', isIntermediary: true  },
                      { name: 'taleo',         isIntermediary: true  } ]
                      
const companies = companyList.reduce((a, v) => (a[+v.isIntermediary].push(v), a), [[], []])

console.log(companies)

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

1 Comment

That's what's up! Thank you!
0

By your code, this is what you want (using ternary to choose whether index 0 or 1)

const companyList = [
    {
        name: 'amazon',
        isIntermediary: false
    },
    {
        name: 'microsoft',
        isIntermediary: false
    },
    {
        name: 'talentsearch',
        isIntermediary: true
    },
    {
        name: 'talent global',
        isIntermediary: true
    },
    {
        name: 'taleo',
        isIntermediary: true
    }
];

const companies = companyList.reduce(
    (acc, curr) => [!curr.isIntermediary ? [...acc[0], curr] : [...acc[0]], curr.isIntermediary ? [...acc[1], curr] : [...acc[1]]],
    [[], []]
);

console.log(companies)

2 Comments

I wanna do a one liner i.e. no curly braces and return
Edited, try now
0

It's not exactly the most readable one-liner, but I think it's what you've asked for.

const companyList = [{
    name: 'amazon',
    isIntermediary: false
  },
  {
    name: 'microsoft',
    isIntermediary: false
  },
  {
    name: 'talentsearch',
    isIntermediary: true
  },
  {
    name: 'talent global',
    isIntermediary: true
  },
  {
    name: 'taleo',
    isIntermediary: true
  }
];

const companies = companyList.reduce((acc, c) => c.isIntermediary ? [acc[0], [...acc[1], c]] : [[...acc[0], c], acc[1]], [[], []]);

console.log(companies)

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.