How to loop through array of objects and make key value pairs?

Viewed 2771

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,
}
4 Answers

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)

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);

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));

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

Related