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,
}
Andrew
  • 149
  • 1
  • 5
  • 12
  • 2
    What specifically are you having problems with? Do you know how to iterate over an array? Do you know how to access properties of an object? Do you know how to add a property to an object? – Felix Kling Jun 12 '20 at 11:41
  • 3
    Possible duplicate of [Convert object array to hash map, indexed by an attribute value of the Object](https://stackoverflow.com/q/26264956/218196) – Felix Kling Jun 12 '20 at 11:43

4 Answers4

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)
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
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);
Rajan
  • 1,512
  • 2
  • 14
  • 18
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));
Siva K V
  • 10,561
  • 2
  • 16
  • 29
  • 1
    Hey, nice answer. It's conceptually similar to my third take (the functional one) https://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. – Matías Fidemraizer Jun 15 '20 at 08:10
  • 1
    Thank you @MatíasFidemraizer, Agree with you. I liked your detailed concepts and answer. – Siva K V Jun 16 '20 at 04:30
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

souenzzo
  • 359
  • 1
  • 6