JavaScript How to convert array to object in Javascript? convert array object to string,convert array object to array

I have the following array:

const x = [
    {event: "oninput", action: ""},
    {event: "onfocus", action: ""}
]

Following is the desired output:

// event: action
{
    oninput: "",
    onfocus: ""
}

I tried the following which didn't really work:

const x = [
    {event: "oninput", action: ""},
    {event: "onfocus", action: ""}
]

console.log({...x.map(y => {return {[y.event]: y.action}})})
Answer:1

You could use Object.assign and map the objects. This collects all objects and assign it to a single object.

const x = [
    { event: "oninput", action: "" },
    { event: "onfocus", action: "" }
];

console.log(Object.assign(...x.map(({ event, action }) => ({ [event]: action }))));
Answer:2

You can use reduce to build a new object. It also gives compatibility back to ECMAScript 2011:

var x = [
    {event: "oninput", action: ""},
    {event: "onfocus", action: ""}
];

var o = x.reduce((acc, obj) => {acc[obj.event] = obj.action; return acc}, {});

console.log(o);
Answer:3

I'm currently teaching myself functional programming. I'm trying to translate the following: (define a 3) (define b (+ a 1)) (* (cond ((> a b) a) ((< a b) b) (else -1)) (+...

I'm currently teaching myself functional programming. I'm trying to translate the following: (define a 3) (define b (+ a 1)) (* (cond ((> a b) a) ((< a b) b) (else -1)) (+...

  1. translate code from one language to another
  2. translate code from java to python
  3. translate code from matlab to python
  4. translate code from r to python
  5. translate code from welsh to english
  6. translate code from python to c++
  7. translate code from sas to r
  8. translate code from java to c#
  9. translate code from spanish to english
  10. translate code from vb.net to c#
  11. translate code from matlab to r
  12. translate code from matlab to julia
  13. translate code from chinese to english
  14. translate from code to english
  15. translate from code
  16. translate code of conduct
  17. translate code for website
  18. translate code in javascript
  19. translate code in python
  20. translate code in php

Using AirBnB's enzyme, we can setState of a component: const loginComponent = shallow(<Login />); loginComponent.setState({ error: true }); I want to do same thing using react-testing-library. ...

Using AirBnB's enzyme, we can setState of a component: const loginComponent = shallow(<Login />); loginComponent.setState({ error: true }); I want to do same thing using react-testing-library. ...

  1. component definition
  2. component to hdmi
  3. component synonym
  4. component form
  5. components of blood
  6. component cable
  7. componentdidupdate
  8. components of fitness
  9. component to hdmi converter
  10. component form of a vector
  11. components of a nucleotide
  12. components of gdp
  13. components of dna
  14. components of physical fitness
  15. component pouch 5e
  16. component speakers
  17. component video
  18. component hardware
  19. components of culture
  20. components of a business plan

I'm using ASP.net 4.5. I have a method like this: protected void OnConfirm(object sender, EventArgs e) { // this function checks if an id exist in database CheckIDExist(123); ...

I'm using ASP.net 4.5. I have a method like this: protected void OnConfirm(object sender, EventArgs e) { // this function checks if an id exist in database CheckIDExist(123); ...

  1. scriptmanager run script after postback
  2. asp.net run script after postback

I have an array: console.log(tenantArray) (8) ["WeWork", "Regus", "Spaces", "Knotel", "RocketSpace", "HQ Global Workspaces", "Other", "Select All"] I also have a large data object, which I want to ...

I have an array: console.log(tenantArray) (8) ["WeWork", "Regus", "Spaces", "Knotel", "RocketSpace", "HQ Global Workspaces", "Other", "Select All"] I also have a large data object, which I want to ...

  1. iterating through array javascript
  2. iterating through array python
  3. iterating through array java
  4. iterating through array c++
  5. iterating through array matlab
  6. iterating through array ruby
  7. iterating through array bash
  8. iterating through array js
  9. iterating through arraylist
  10. iterating through array php
  11. iterating through array swift
  12. iterating through array of objects javascript
  13. iterating through array powershell
  14. iterating through array in typescript
  15. iterating through array in scala
  16. iterating through array in perl