javascript - How to loop through array of objects and make key value pairs? - Stack Overflow

I have an array of objects like this: let someObj = {items: [{id: '12',value: true}, {id: �

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

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,
}
Share Improve this question asked Jun 12, 2020 at 11:39 AndrewAndrew 1491 gold badge6 silver badges12 bronze badges 2
  • 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 Commented Jun 12, 2020 at 11:41
  • 3 Possible duplicate of Convert object array to hash map, indexed by an attribute value of the Object – Felix Kling Commented Jun 12, 2020 at 11:43
Add a ment  | 

4 Answers 4

Reset to default 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 pose = 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 = pose (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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744244275a4564850.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信