2

I'm trying to turn this data

[
  {
    key: 'myvar',
    value: 'my value',
    global: false,
  },
  {
    key: 'foo',
    value: 'bar',
    global: false,
  },
  {
    key: 'featureEnabled',
    value: true,
    global: true,
    accountId: 123,

  },
]

into this

{
  myvar: 'my value'
  foo: 'bar'
  123: { 'featureEnabled': true }
}

I was thinking something along the lines of something like below but this will return undefined for objects that don't have an accountId property. I thought there might be a way to use an if statement here.

const globalResponse = Object.assign({}, ...getPreference.map(o => ({ [o.accountId]: { [o.key]: o.value } })))
1
  • Use a for loop with an if condition. Object.assign with a map and a ternary will make it unreadable. Commented Jun 23, 2020 at 15:49

2 Answers 2

1

You can use of reduce to build your new object and setup specific triggers for specifics use cases.

function mutateObject(tmpBasis, key, value) {
  tmpBasis[key] = value;
  
  return tmpBasis;
}

const ret = [{
    key: 'myvar',
    value: 'my value',
    global: false,
  },
  {
    key: 'foo',
    value: 'bar',
    global: false,
  },
  {
    key: 'featureEnabled',
    value: true,
    global: true,
    accountId: 123,
  },
  {
    key: 'otherKey',
    value: true,
    global: true,
    accountId: 123,
  },
  {
    key: 'featureEnabled',
    value: true,
    global: true,
    accountId: 512,
  },
].reduce((tmp, {
  key,
  value,
  global,
  accountId,
}) => ({
  ...tmp,

  // If we are in global case, we either create an empty object containing the new key
  // or we add the new key inside of the existing object
  [global ? accountId : key]: global ? mutateObject(tmp[accountId] || {}, key, value) : value,
}), {});

console.log(ret);

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

6 Comments

Thanks so much but I have a question. When I have two objects that have global set to true reduce is only returning the first object in the array. For example [ { key: 'featureEnabled',value: true,global: true,accountId: 123, }, { key: 'foo',value: 'bar' , global: true,accountId: 123, } ]
@Croisade It's because the accountId that is used as a key is the same! If you want to keep both value, either have different accountId or change the code to create an array behind the accoundId key.
How would I change the code to create an array behind accountId? Sorry, a bit new.
@Croisade actually after rethinking your case, a simple object is a more suitable solution. See my edited code
Thanks a lot for the help. I would really like to get better at stuff like this. Do you happen to know any resources I can use or maybe some keywords I can google so I can study this a bit more?
|
0

You can use reduce() with a condition inside like below

var myJSON = [{
    key: 'myvar',
    value: 'my value',
    global: false,
  },
  {
    key: 'foo',
    value: 'bar',
    global: false,
  },
  {
    key: 'featureEnabled',
    value: true,
    global: true,
    accountId: 123,

  },
];

let newJSON = myJSON.reduce((target, item) => {
  !!item.accountId ? target[item.accountId] = { [item.key]: item.value } : target[item.key] = item.value;
  return target;
}, {});

console.log(newJSON);

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.