3

I have an array of objects like this:

let someObj = {
    items: [{
        id: '12',
        value: true
    }, {
        id: '34',
        value: true
    }, {
        id: '56',
        value: false
    }]

}

I want add this to an exiting object, where id is a key of this object, like this:

let obj = {
    someKey: someValue,
    '12': true,
    '34': true,
    '56': false,
}
2

4 Answers 4

4

You may achieve your goal using Array#reduce as follows:

const input = {
  items: [{
    id: '12',
    value: true
  }, {
    id: '34',
    value: true
  }, {
    id: '56',
    value: false
  }]
}

const output = input.items.reduce((o, {
  id,
  value
}) => (o[id] = value, o), {})

console.log(output)

Also, and maybe the simplest approach might be using Array#map to turn objects into pairs and then convert them into an object using Object.fromPairs:

const input = {
  items: [{
    id: '12',
    value: true
  }, {
    id: '34',
    value: true
  }, {
    id: '56',
    value: false
  }]
}

const output = Object.fromEntries(input.items.map(({
  id,
  value
}) => [id, value]))

console.log(output)

Finally, here's a functional approach:

  // Composes two functions
  const compose = f => g => x => f (g (x))

  // Given the key-value pairs of some object with 2 properties, maps a pair of values
  const values = ([[, x], [, y]]) => [x, y]

  // Turns an object of two properties into a pair of property values
  const entry = compose (values) (Object.entries)

  // Turns many objects of two properties, into an object on which
  // keys are first properties' values, and vaules the second properties' values.
  const keyValueObject = xs => Object.fromEntries (xs.map (entry))

  const input = {
    items: [{
      id: '12',
      value: true
    }, {
      id: '34',
      value: true
    }, {
      id: '56',
      value: false
    }]
  }

  const output = keyValueObject (input.items)

  console.log(output)

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

Comments

1

You can iterate each item from items and create a new object as shown below.

let someObj = {
    items: [{
        id: '12',
        value: true
    }, {
        id: '34',
        value: true
    }, {
        id: '56',
        value: false
    }]

}
const newObj = {};
someObj.items.map(item =>{
newObj[item.id]= item.value;
});

console.log(newObj);

Comments

1

Use map and Object.values will simplify.

const output = arr => Object.fromEntries(arr.map(Object.values));

let someObj = {
  items: [
    {
      id: "12",
      value: true,
    },
    {
      id: "34",
      value: true,
    },
    {
      id: "56",
      value: false,
    },
  ],
};


console.log(output(someObj.items));

2 Comments

Hey, nice answer. It's conceptually similar to my third take (the functional one) stackoverflow.com/a/62343622/411632 but yours is the simplest. Perfect unless items have more than 2 properties. In that case, my fixed mapping would still work. Anyway, your answer should be the one choosen by the OP.
Thank you @MatíasFidemraizer, Agree with you. I liked your detailed concepts and answer.
0

First, you can transform the itens into "KV" entries

> someObj.items.map(({id, value}) => [id, value])
[ [ '12', true ], [ '34', true ], [ '56', false ] ]

Then turn it into Object

> Object.fromEntries(someObj.items.map(({id, value}) => [id, value]))
{ '12': true, '34': true, '56': false }

You can do a function

> let ObjectFromMapping = (vs, mapping) => Object.fromEntries(vs.map(mapping))
> ObjectFromMapping(someObj.items, ({id, value}) => [id, value])
{ '12': true, '34': true, '56': false }

Maybe turn vs into a iterable is a good idea

> let ObjectFromMapping = (vs, mapping) => Object.fromEntries([... vs].map(mapping))
> ObjectFromMapping("abc", (char, idx) =>  [idx, char])
{ '0': 'a', '1': 'b', '2': 'c' }

Then your function will work on any iterable

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.